Skip to content

Commit 3f32d4e

Browse files
authored
Merge pull request #526 from analogdevicesinc/rgetz-fix-xml-entities
xml: Add capability to properly encode xml entities in context attrib…
2 parents 89c1eb5 + 71bdda5 commit 3f32d4e

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

context.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
147169
err_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

iio-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ void free_device(struct iio_device *dev);
263263
char *iio_channel_get_xml(const struct iio_channel *chn, size_t *len);
264264
char *iio_device_get_xml(const struct iio_device *dev, size_t *len);
265265

266+
char *encode_xml_ndup(const char * input);
266267
char *iio_context_create_xml(const struct iio_context *ctx);
267268
int iio_context_init(struct iio_context *ctx);
268269

xml.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
#include <libxml/tree.h>
2424
#include <string.h>
2525

26+
/* 'input' must be in UTF-8 encoded, null terminated */
27+
char * encode_xml_ndup(const char * input)
28+
{
29+
char * out;
30+
31+
out = (char *)xmlEncodeEntitiesReentrant(NULL, (const xmlChar *)input);
32+
33+
return out;
34+
}
35+
2636
static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
2737
{
2838
xmlAttr *attr;

0 commit comments

Comments
 (0)