Skip to content

Commit eef0f77

Browse files
rgetzpcercuei
authored andcommitted
set errno to 0, and check after calling library functions
A few times we are using strtol and strtoul, where we were not setting errno to zero, and then checking it afterwards - this could lead to undefined behaviour based on end user inputs, so do like the man page suggests. Signed-off-by: Robin Getz <[email protected]>
1 parent a037c83 commit eef0f77

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

network.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,10 @@ struct iio_context * network_create_context(const char *host)
11261126
}
11271127
if (port) {
11281128
unsigned long int tmp;
1129+
1130+
errno = 0;
11291131
tmp = strtoul(port, &end, 0);
1130-
if (port == end || tmp > 0xFFFF) {
1132+
if (port == end || tmp > 0xFFFF || errno == ERANGE) {
11311133
errno = ENOENT;
11321134
return NULL;
11331135
}

usb.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,8 +1222,9 @@ static int parse_vid_pid(const char *vid_pid, uint16_t *vid, uint16_t *pid)
12221222
if (!vid_pid)
12231223
return 0;
12241224

1225+
errno = 0;
12251226
val = strtoul(vid_pid, &ptr, 16);
1226-
if (ptr == vid_pid || val > 0xFFFF || *ptr != ':')
1227+
if (ptr == vid_pid || val > 0xFFFF || *ptr != ':' || errno == ERANGE)
12271228
return -EINVAL;
12281229

12291230
*vid = (uint16_t) val;
@@ -1233,8 +1234,9 @@ static int parse_vid_pid(const char *vid_pid, uint16_t *vid, uint16_t *pid)
12331234
if (*vid_pid == '*')
12341235
return vid_pid[1] == '\0' ? 0 : -EINVAL;
12351236

1237+
errno = 0;
12361238
val = strtoul(vid_pid, &ptr, 16);
1237-
if (ptr == vid_pid || val > 0xFFFF || *ptr != '\0')
1239+
if (ptr == vid_pid || val > 0xFFFF || *ptr != '\0' || errno == ERANGE)
12381240
return -EINVAL;
12391241

12401242
*pid = (uint16_t) val;

xml.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,14 @@ static struct iio_context * iio_create_xml_context_helper(xmlDoc *doc)
428428
if (!strcmp((char *) attr->name, "description")) {
429429
description = content;
430430
} else if (!strcmp((char *) attr->name, "version-major")) {
431+
errno = 0;
431432
major = strtol(content, &end, 10);
432-
if (*end != '\0')
433+
if (*end != '\0' || errno == ERANGE)
433434
IIO_WARNING("invalid format for major version\n");
434435
} else if (!strcmp((char *) attr->name, "version-minor")) {
436+
errno = 0;
435437
minor = strtol(content, &end, 10);
436-
if (*end != '\0')
438+
if (*end != '\0' || errno == ERANGE)
437439
IIO_WARNING("invalid format for minor version\n");
438440
} else if (!strcmp((char *) attr->name, "version-git")) {
439441
git_tag = content;

0 commit comments

Comments
 (0)