From 23ab5d8b48da8ce7294bdde71d01617a120d4d30 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Tue, 7 Apr 2020 12:36:18 -0400 Subject: [PATCH 1/3] coverity: fix : Resource leak (RESOURCE_LEAK) On error, data was not freed. Signed-off-by: Robin Getz --- dns_sd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dns_sd.c b/dns_sd.c index 8afe92124..0fb81d884 100644 --- a/dns_sd.c +++ b/dns_sd.c @@ -286,13 +286,14 @@ int dnssd_discover_host(char *addr_str, size_t addr_len, uint16_t *port) ret = dnssd_find_hosts(&ddata); if (ret < 0) - return ret; + goto host_fail; if (ddata) { *port = ddata->port; strncpy(addr_str, ddata->addr_str, addr_len); } +host_fail: dnssd_free_all_discovery_data(ddata); /* negative error codes, 0 for no data */ From e12120a060c7ec0ee200295338860637b63dddd9 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Tue, 7 Apr 2020 13:51:51 -0400 Subject: [PATCH 2/3] Coverity: fix Call to function atoi with tainted argument by using a different function that does better errror handling. Signed-off-by: Robin Getz --- tests/iio_stresstest.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/iio_stresstest.c b/tests/iio_stresstest.c index 12dc5a345..0c074db68 100644 --- a/tests/iio_stresstest.c +++ b/tests/iio_stresstest.c @@ -385,15 +385,18 @@ int main(int argc, char **argv) break; case 'b': info.arg_index += 2; - info.buffer_size = atoi(info.argv[info.arg_index]); + info.buffer_size = strtol(info.argv[info.arg_index], NULL, 10); + /* Max 4M */ + if (info.buffer_size > (1024 * 1024 * 4)) + info.buffer_size = 1024 * 1024 * 4; break; case 't': info.arg_index +=2; - info.timeout = 1000 * atoi(info.argv[info.arg_index]); + info.timeout = 1000 * strtol(info.argv[info.arg_index], NULL, 10); break; case 'T': info.arg_index +=2; - info.num_threads = atoi(info.argv[info.arg_index]); + info.num_threads = strtol(info.argv[info.arg_index], NULL, 10); break; case 'v': if (!info.verbose) From 7d39af6d7e4b1c45eea9b33fdec15da39cfb517c Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Tue, 7 Apr 2020 14:54:36 -0400 Subject: [PATCH 3/3] coverity: fix Unchecked return value from library fcntl can return errors (as -1, and set errno); so handle that appropraitely. Signed-off-by: Robin Getz --- iiod/iiod.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/iiod/iiod.c b/iiod/iiod.c index 5b4309cda..aad84e7aa 100644 --- a/iiod/iiod.c +++ b/iiod/iiod.c @@ -407,9 +407,24 @@ static int main_interactive(struct iio_context *ctx, bool verbose, bool use_aio) if (!use_aio) { flags = fcntl(STDIN_FILENO, F_GETFL); - fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); + if (flags >= 0) + flags = fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); + if (flags < 0) { + char err_str[1024]; + iio_strerror(errno, err_str, sizeof(err_str)); + IIO_ERROR("Could not get/set O_NONBLOCK on STDIN_FILENO" + " %s (%d)\n", err_str, -errno); + } + flags = fcntl(STDOUT_FILENO, F_GETFL); - fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); + if (flags >= 0) + flags = fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); + if (flags < 0) { + char err_str[1024]; + iio_strerror(errno, err_str, sizeof(err_str)); + IIO_ERROR("Could not get/set O_NONBLOCK on STDOUT_FILENO" + " %s (%d)\n", err_str, -errno); + } } interpreter(ctx, STDIN_FILENO, STDOUT_FILENO, verbose,