@@ -50,6 +50,7 @@ char * iio_context_create_xml(const struct iio_context *ctx)
5050 ssize_t len ;
5151 size_t * devices_len = NULL ;
5252 char * str , * ptr , * eptr , * * devices = NULL ;
53+ char * * ctx_attrs , * * ctx_values ;
5354 unsigned int i ;
5455
5556 len = sizeof (xml_header ) - 1 ;
@@ -61,17 +62,33 @@ char * iio_context_create_xml(const struct iio_context *ctx)
6162 len += sizeof (" description=\"\"" ) - 1 ;
6263 }
6364
65+ ctx_attrs = calloc (ctx -> nb_attrs , sizeof (ctx -> attrs ));
66+ if (!ctx_attrs ) {
67+ errno = ENOMEM ;
68+ return NULL ;
69+ }
70+ ctx_values = calloc (ctx -> nb_attrs , sizeof (ctx -> values ));
71+ if (!ctx_values ) {
72+ errno = ENOMEM ;
73+ goto err_free_ctx_attrs ;
74+ }
75+
6476 for (i = 0 ; i < ctx -> nb_attrs ; i ++ ) {
65- len += strnlen (ctx -> attrs [i ], MAX_ATTR_NAME );
66- len += strnlen (ctx -> values [i ], MAX_ATTR_VALUE );
77+ ctx_attrs [i ] = encode_xml_ndup (ctx -> attrs [i ]);
78+ ctx_values [i ] = encode_xml_ndup (ctx -> values [i ]);
79+ if (!ctx_attrs [i ] || !ctx_values [i ])
80+ goto err_free_ctx_attrs_values ;
81+
82+ len += strnlen (ctx_attrs [i ], MAX_ATTR_NAME );
83+ len += strnlen (ctx_values [i ], MAX_ATTR_VALUE );
6784 len += sizeof ("<context-attribute name=\"\" value=\"\" />" ) - 1 ;
6885 }
6986
7087 if (ctx -> nb_devices ) {
7188 devices_len = malloc (ctx -> nb_devices * sizeof (* devices_len ));
7289 if (!devices_len ) {
7390 errno = ENOMEM ;
74- return NULL ;
91+ goto err_free_ctx_attrs_values ;
7592 }
7693
7794 devices = calloc (ctx -> nb_devices , sizeof (* devices ));
@@ -111,10 +128,15 @@ char * iio_context_create_xml(const struct iio_context *ctx)
111128
112129 for (i = 0 ; i < ctx -> nb_attrs && len > 0 ; i ++ ) {
113130 ptr += iio_snprintf (ptr , len , "<context-attribute name=\"%s\" value=\"%s\" />" ,
114- ctx -> attrs [i ], ctx -> values [i ]);
131+ ctx_attrs [i ], ctx_values [i ]);
132+ free (ctx_attrs [i ]);
133+ free (ctx_values [i ]);
115134 len = eptr - ptr ;
116135 }
117136
137+ free (ctx_attrs );
138+ free (ctx_values );
139+
118140 for (i = 0 ; i < ctx -> nb_devices ; i ++ ) {
119141 if (len > (ssize_t ) devices_len [i ]) {
120142 memcpy (ptr , devices [i ], devices_len [i ]); /* Flawfinder: ignore */
@@ -146,6 +168,17 @@ char * iio_context_create_xml(const struct iio_context *ctx)
146168 free (devices );
147169err_free_devices_len :
148170 free (devices_len );
171+ err_free_ctx_attrs_values :
172+ for (i = 0 ; i < ctx -> nb_attrs ; i ++ ) {
173+ if (ctx_attrs [i ])
174+ free (ctx_attrs [i ]);
175+ if (ctx_values [i ])
176+ free (ctx_values [i ]);
177+ }
178+
179+ free (ctx_values );
180+ err_free_ctx_attrs :
181+ free (ctx_attrs );
149182 return NULL ;
150183}
151184
0 commit comments