Skip to content

Commit

Permalink
utils: catch calloc memory failures in iio_stresstest
Browse files Browse the repository at this point in the history
On error, calloc() returns NULL. Or a successful call to calloc() with nmemb
or size equal to zero.

In the past, we would not catch that, and just start operating on the
null pointer (crashing). So catch that, and gracefully error out.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz authored and pcercuei committed Apr 28, 2023
1 parent 91e5f97 commit 8eda862
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/iio_stresstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ int main(int argc, char **argv)

ret = (void *)calloc(info.num_threads, sizeof(void *));

if (!ret || !info.start || !info.refills || !info.buffers || !info.starts ||
!info.tid || !info.threads) {
fprintf(stderr, "Memory allocation failure\n");
return 0;
}

for (i = 0; i < info.num_threads; i++) {
info.start[i] = malloc(NUM_TIMESTAMPS * sizeof(struct timeval));
}
Expand Down Expand Up @@ -658,6 +664,11 @@ int main(int argc, char **argv)
/* gather and sort things, so we can print out a histogram */
struct timeval *sort;
sort = calloc(info.num_threads * NUM_TIMESTAMPS, sizeof(struct timeval));
if (!sort) {
app_running = 0;
fprintf(stderr, "Memory allocation failure\n");
break;
}
b = 0;
/* gather */
for (i = 0; i < info.num_threads; i++) {
Expand Down

0 comments on commit 8eda862

Please sign in to comment.