Skip to content

Commit

Permalink
libutil/cleanup: fix unchecked malloc
Browse files Browse the repository at this point in the history
Problem: if calloc() fails in cleanup_push(), a
segfault will ensue.

Don't assign values to allocated struct if calloc fails.
Also, if zlist_append() fails, free the allocated struct
instead of leaking it.
  • Loading branch information
garlick committed Feb 28, 2020
1 parent 5118f44 commit 53a3de1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/common/libutil/cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ void cleanup_push (cleaner_fun_f *fun, void *arg)
cleaner_pid = getpid ();
atexit (cleanup_run);
}
/* Ignore error, no way to return it to caller anyway... */
struct cleaner *c = calloc (sizeof (struct cleaner), 1);
c->fun = fun;
c->arg = arg;
/* Ignore return code, no way to return it to caller anyway... */
(void)zlist_push (cleanup_list, c);
if (c) {
c->fun = fun;
c->arg = arg;
if (zlist_push (cleanup_list, c) < 0)
free (c);
}
pthread_mutex_unlock (&mutex);
}

Expand Down

0 comments on commit 53a3de1

Please sign in to comment.