Skip to content

Commit 4c577b3

Browse files
committed
Flawfinder : remove atol and atof, replace with strtoll and strtof
Flawfinder reminds us that unless checked, the resulting number can exceed the expected range (CWE-190). If source untrusted, check both minimum and maximum, even if the input had no minus sign (large numbers can roll over into negative number; consider saving to an unsigned value if that is intended). https://cwe.mitre.org/data/definitions/190.html replace these library calls with strtoll and strtof, and include more error checking. Signed-off-by: Robin Getz <[email protected]>
1 parent 8606010 commit 4c577b3

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

local.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,16 @@ static int handle_protected_scan_element_attr(struct iio_channel *chn,
12211221

12221222
if (!strcmp(name, "index")) {
12231223
ret = local_read_dev_attr(dev, path, buf, sizeof(buf), false);
1224-
if (ret > 0)
1225-
chn->index = atol(buf);
1224+
if (ret > 0) {
1225+
char *end;
1226+
long long value;
1227+
1228+
value = strtoll(buf, &end, 0);
1229+
if (end == buf || value < 0 || value > LONG_MAX)
1230+
return -EINVAL;
12261231

1232+
chn->index = (long) value;
1233+
}
12271234
} else if (!strcmp(name, "type")) {
12281235
ret = local_read_dev_attr(dev, path, buf, sizeof(buf), false);
12291236
if (ret > 0) {
@@ -1917,16 +1924,21 @@ static const struct iio_backend_ops local_ops = {
19171924

19181925
static void init_data_scale(struct iio_channel *chn)
19191926
{
1920-
char buf[1024];
1927+
char *end, buf[1024];
19211928
ssize_t ret;
1929+
float value;
19221930

1931+
chn->format.with_scale = false;
19231932
ret = iio_channel_attr_read(chn, "scale", buf, sizeof(buf));
1924-
if (ret < 0) {
1925-
chn->format.with_scale = false;
1926-
} else {
1927-
chn->format.with_scale = true;
1928-
chn->format.scale = atof(buf);
1929-
}
1933+
if (ret < 0)
1934+
return;
1935+
1936+
value = strtof(buf, &end);
1937+
if (end == buf)
1938+
return;
1939+
1940+
chn->format.with_scale = true;
1941+
chn->format.scale = value;
19301942
}
19311943

19321944
static void init_scan_elements(struct iio_context *ctx)

xml.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
139139
const char *name = (const char *) attr->name,
140140
*content = (const char *) attr->children->content;
141141
if (!strcmp(name, "index")) {
142-
chn->index = atol(content);
142+
char *end;
143+
long long value;
144+
145+
value = strtoll(content, &end, 0);
146+
if (end == content || value < 0 || value > LONG_MAX)
147+
return;
148+
chn->index = (long) value;
143149
} else if (!strcmp(name, "format")) {
144150
char e, s;
145151
if (strchr(content, 'X')) {
@@ -170,8 +176,17 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
170176
chn->format.is_fully_defined = (s == 'S' || s == 'U' ||
171177
chn->format.bits == chn->format.length);
172178
} else if (!strcmp(name, "scale")) {
179+
char *end;
180+
float value;
181+
182+
value = strtof(content, &end);
183+
if (end == content) {
184+
chn->format.with_scale = false;
185+
return;
186+
}
187+
173188
chn->format.with_scale = true;
174-
chn->format.scale = atof(content);
189+
chn->format.scale = value;
175190
} else {
176191
IIO_WARNING("Unknown attribute \'%s\' in <scan-element>\n",
177192
name);

0 commit comments

Comments
 (0)