Skip to content

Commit

Permalink
xml: Properly handle errors in setup_scan_element
Browse files Browse the repository at this point in the history
It would previously silently ignore all errors.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Apr 22, 2021
1 parent 906b8d0 commit cb01ccd
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ static int add_attr_to_device(struct iio_device *dev, xmlNode *n, enum iio_attr_
}
}

static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
static int setup_scan_element(struct iio_channel *chn, xmlNode *n)
{
xmlAttr *attr;
int err;

for (attr = n->properties; attr; attr = attr->next) {
const char *name = (const char *) attr->name,
Expand All @@ -102,12 +103,12 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
errno = 0;
value = strtoll(content, &end, 0);
if (end == content || value < 0 || errno == ERANGE)
return;
return -EINVAL;
chn->index = (long) value;
} else if (!strcmp(name, "format")) {
char e, s;
if (strchr(content, 'X')) {
iio_sscanf(content, "%ce:%c%u/%uX%u>>%u",
err = iio_sscanf(content, "%ce:%c%u/%uX%u>>%u",
#ifdef _MSC_BUILD
&e, (unsigned int)sizeof(e),
&s, (unsigned int)sizeof(s),
Expand All @@ -118,9 +119,11 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
&chn->format.length,
&chn->format.repeat,
&chn->format.shift);
if (err != 6)
return -EINVAL;
} else {
chn->format.repeat = 1;
iio_sscanf(content, "%ce:%c%u/%u>>%u",
err = iio_sscanf(content, "%ce:%c%u/%u>>%u",
#ifdef _MSC_BUILD
&e, (unsigned int)sizeof(e),
&s, (unsigned int)sizeof(s),
Expand All @@ -130,6 +133,8 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
&chn->format.bits,
&chn->format.length,
&chn->format.shift);
if (err != 5)
return -EINVAL;
}
chn->format.is_be = e == 'b';
chn->format.is_signed = (s == 's' || s == 'S');
Expand All @@ -143,7 +148,7 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
value = strtof(content, &end);
if (end == content || errno == ERANGE) {
chn->format.with_scale = false;
return;
return -EINVAL;
}

chn->format.with_scale = true;
Expand All @@ -153,6 +158,8 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
name);
}
}

return 0;
}

static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n)
Expand Down

0 comments on commit cb01ccd

Please sign in to comment.