Skip to content

Commit b31fc3e

Browse files
committed
examples: Improved error handling when malloc or calloc fails.
coverity pointed out the error handling a few of the examples didn't exist. So add the common things that it found. Signed-off-by: Robin Getz <[email protected]>
1 parent 0864ab8 commit b31fc3e

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

examples/dummy-iiostream.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ int main (int argc, char **argv)
368368
size_t sample_size = fmt->length / 8 * repeat;
369369

370370
buf = malloc(sample_size * buffer_length);
371+
if (!buf) {
372+
perror("trying to allocate memory for buffer\n");
373+
shutdown();
374+
}
371375

372376
if (buffer_read_method == CHANNEL_READ_RAW)
373377
bytes = iio_channel_read_raw(channels[i], rxbuf, buf, sample_size * buffer_length);

examples/iio-monitor.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ static struct iio_context *show_contexts_screen(void)
246246
return NULL;
247247

248248
screen = initCDKScreen(win);
249+
if (!screen) {
250+
fprintf(stderr, "out of memory\n");
251+
goto scan_err;
252+
}
249253

250254
do {
251255
ret = iio_scan_context_get_info_list(scan_ctx, &info);
@@ -255,6 +259,10 @@ static struct iio_context *show_contexts_screen(void)
255259
num_contexts = ret;
256260

257261
items = calloc(num_contexts + 1, sizeof(*items));
262+
if (!items) {
263+
fprintf(stderr, "calloc failed, out of memory\n");
264+
break;
265+
}
258266

259267
for (i = 0; i < num_contexts; i++) {
260268
asprintf(&items[i], "</%d>%s<!%d> </%d>[%s]<!%d>", YELLOW,
@@ -307,6 +315,7 @@ static struct iio_context *show_contexts_screen(void)
307315

308316
destroyCDKScreen(screen);
309317

318+
scan_err:
310319
iio_scan_context_destroy(scan_ctx);
311320

312321
return ctx;
@@ -322,11 +331,23 @@ static void show_main_screen(struct iio_context *ctx)
322331

323332
stop = FALSE;
324333
screen = initCDKScreen(left);
334+
if (!screen) {
335+
stop = TRUE;
336+
fprintf(stderr, "initCDKScreen failed, out of memory\n");
337+
return;
338+
}
325339

326-
pthread_create(&thd, NULL, read_thd, ctx);
340+
if (pthread_create(&thd, NULL, read_thd, ctx)) {
341+
fprintf(stderr, "problem with pthread_create\n");
342+
goto thread_err;
343+
}
327344

328345
nb_devices = iio_context_get_devices_count(ctx);
329-
dev_names = malloc(nb_devices * sizeof(char *));
346+
dev_names = calloc(nb_devices, sizeof(char *));
347+
if (!dev_names) {
348+
fprintf(stderr, "calloc failed, out of memory\n");
349+
goto name_err;
350+
}
330351

331352
for (i = 0; i < nb_devices; i++) {
332353
char buf[1024];
@@ -336,6 +357,8 @@ static void show_main_screen(struct iio_context *ctx)
336357
name = iio_device_get_id(dev);
337358
sprintf(buf, "</B> %s", name);
338359
dev_names[i] = strdup(buf);
360+
if (!dev_names[i])
361+
goto dev_name_err;
339362
}
340363

341364
boxWindow(right, 0);
@@ -360,6 +383,20 @@ static void show_main_screen(struct iio_context *ctx)
360383
free(dev_names[i]);
361384
free(dev_names);
362385
destroyCDKScreen(screen);
386+
387+
dev_name_err:
388+
for (i = 0; i < nb_devices; i++) {
389+
if (dev_names[i])
390+
free(dev_names[i]);
391+
}
392+
free(dev_names);
393+
name_err:
394+
stop = TRUE;
395+
pthread_join(thd, NULL);
396+
thread_err:
397+
destroyCDKScreen(screen);
398+
399+
return;
363400
}
364401

365402
int main(void)

0 commit comments

Comments
 (0)