Skip to content

Commit

Permalink
Coverity: Fix potential divide by zero.
Browse files Browse the repository at this point in the history
This code in iio_writedev.c:
216    size_t sample_size;
...
368    sample_size = iio_device_get_sample_size(dev);
...
404        num_samples -= write_len / sample_size;

There are two issues:

iio_device_get_sample_size can return negative error codes,
and we would miss these. Fix that by making it a signed number.

this could potentially cause an error if sample_size is zero.
Catch that fault and error out. While we are here, check
for negative error codes, and also error out.

Reported by Coverity.

Signed-off-by: Robin Getz <[email protected]>

foo

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Apr 23, 2020
1 parent 0864ab8 commit 71a5b00
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ int main(int argc, char **argv)
const char *arg_ip = NULL;
int c, option_index = 0;
struct iio_device *dev;
size_t sample_size;
ssize_t sample_size;
int timeout = -1;
bool scan_for_context = false;
bool cyclic_buffer = false;
Expand Down Expand Up @@ -366,6 +366,18 @@ int main(int argc, char **argv)
}

sample_size = iio_device_get_sample_size(dev);
/* Zero isn't normally an error code, but in this case it is an error */
if (sample_size == 0) {
fprintf(stderr, "Unable to get sample size, returned 0\n");
iio_context_destroy(ctx);
return EXIT_FAILURE;
} else if (sample_size < 0) {
char buf[256];
iio_strerror(errno, buf, sizeof(buf));
fprintf(stderr, "Unable to get sample size : %s\n", buf);
iio_context_destroy(ctx);
return EXIT_FAILURE;
}

buffer = iio_device_create_buffer(dev, buffer_size, cyclic_buffer);
if (!buffer) {
Expand Down

0 comments on commit 71a5b00

Please sign in to comment.