Skip to content

Commit 71a5b00

Browse files
committed
Coverity: Fix potential divide by zero.
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]>
1 parent 0864ab8 commit 71a5b00

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

tests/iio_writedev.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ int main(int argc, char **argv)
213213
const char *arg_ip = NULL;
214214
int c, option_index = 0;
215215
struct iio_device *dev;
216-
size_t sample_size;
216+
ssize_t sample_size;
217217
int timeout = -1;
218218
bool scan_for_context = false;
219219
bool cyclic_buffer = false;
@@ -366,6 +366,18 @@ int main(int argc, char **argv)
366366
}
367367

368368
sample_size = iio_device_get_sample_size(dev);
369+
/* Zero isn't normally an error code, but in this case it is an error */
370+
if (sample_size == 0) {
371+
fprintf(stderr, "Unable to get sample size, returned 0\n");
372+
iio_context_destroy(ctx);
373+
return EXIT_FAILURE;
374+
} else if (sample_size < 0) {
375+
char buf[256];
376+
iio_strerror(errno, buf, sizeof(buf));
377+
fprintf(stderr, "Unable to get sample size : %s\n", buf);
378+
iio_context_destroy(ctx);
379+
return EXIT_FAILURE;
380+
}
369381

370382
buffer = iio_device_create_buffer(dev, buffer_size, cyclic_buffer);
371383
if (!buffer) {

0 commit comments

Comments
 (0)