Commit e9e45b43 authored by Hartmut Knaack's avatar Hartmut Knaack Committed by Jonathan Cameron

tools:iio: catch errors in string allocation

This patch catches errors in string allocation in generic_buffer.c,
iio_event_monitor.c, iio_utils.c and lsiio.c.
Signed-off-by: default avatarHartmut Knaack <knaack.h@gmx.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent 2156b179
...@@ -234,7 +234,9 @@ int main(int argc, char **argv) ...@@ -234,7 +234,9 @@ int main(int argc, char **argv)
} }
printf("iio device number being used is %d\n", dev_num); printf("iio device number being used is %d\n", dev_num);
asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
if (ret < 0)
return -ENOMEM;
if (!notrigger) { if (!notrigger) {
if (trigger_name == NULL) { if (trigger_name == NULL) {
......
...@@ -265,6 +265,8 @@ int main(int argc, char **argv) ...@@ -265,6 +265,8 @@ int main(int argc, char **argv)
/* If we can't find a IIO device by name assume device_name is a /* If we can't find a IIO device by name assume device_name is a
IIO chrdev */ IIO chrdev */
chrdev_name = strdup(device_name); chrdev_name = strdup(device_name);
if (!chrdev_name)
return -ENOMEM;
} }
fd = open(chrdev_name, 0); fd = open(chrdev_name, 0);
......
...@@ -36,7 +36,7 @@ int iioutils_break_up_name(const char *full_name, ...@@ -36,7 +36,7 @@ int iioutils_break_up_name(const char *full_name,
char *current; char *current;
char *w, *r; char *w, *r;
char *working, *prefix = ""; char *working, *prefix = "";
int i; int i, ret;
for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++) for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
if (!strncmp(full_name, iio_direction[i], if (!strncmp(full_name, iio_direction[i],
...@@ -46,6 +46,9 @@ int iioutils_break_up_name(const char *full_name, ...@@ -46,6 +46,9 @@ int iioutils_break_up_name(const char *full_name,
} }
current = strdup(full_name + strlen(prefix) + 1); current = strdup(full_name + strlen(prefix) + 1);
if (!current)
return -ENOMEM;
working = strtok(current, "_\0"); working = strtok(current, "_\0");
w = working; w = working;
...@@ -59,10 +62,10 @@ int iioutils_break_up_name(const char *full_name, ...@@ -59,10 +62,10 @@ int iioutils_break_up_name(const char *full_name,
r++; r++;
} }
*w = '\0'; *w = '\0';
asprintf(generic_name, "%s_%s", prefix, working); ret = asprintf(generic_name, "%s_%s", prefix, working);
free(current); free(current);
return 0; return (ret == -1) ? -ENOMEM : 0;
} }
/** /**
......
...@@ -107,7 +107,12 @@ static void dump_devices(void) ...@@ -107,7 +107,12 @@ static void dump_devices(void)
if (check_prefix(ent->d_name, type_device)) { if (check_prefix(ent->d_name, type_device)) {
char *dev_dir_name; char *dev_dir_name;
asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name); if (asprintf(&dev_dir_name, "%s%s", iio_dir,
ent->d_name) < 0) {
printf("Memory allocation failed\n");
goto error_close_dir;
}
dump_one_device(dev_dir_name); dump_one_device(dev_dir_name);
free(dev_dir_name); free(dev_dir_name);
if (verblevel >= VERBLEVEL_SENSORS) if (verblevel >= VERBLEVEL_SENSORS)
...@@ -119,11 +124,17 @@ static void dump_devices(void) ...@@ -119,11 +124,17 @@ static void dump_devices(void)
if (check_prefix(ent->d_name, type_trigger)) { if (check_prefix(ent->d_name, type_trigger)) {
char *dev_dir_name; char *dev_dir_name;
asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name); if (asprintf(&dev_dir_name, "%s%s", iio_dir,
ent->d_name) < 0) {
printf("Memory allocation failed\n");
goto error_close_dir;
}
dump_one_trigger(dev_dir_name); dump_one_trigger(dev_dir_name);
free(dev_dir_name); free(dev_dir_name);
} }
} }
error_close_dir:
closedir(dp); closedir(dp);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment