libmthread: resolve memory leaks on exception path

If libmthread runs into a memory allocation failure while attempting
to enlarge its thread pool, it does not free up any preliminary
allocations made so far.

Reported by dcb314.

This closes #152.

Change-Id: Ib882a4544e4802a0eb0a53446b43997876cde633
This commit is contained in:
David van Moolenbroek 2016-08-05 10:26:36 +00:00
parent ab712d1923
commit 815afbad33

View File

@ -194,12 +194,22 @@ static int mthread_increase_thread_pool(void)
new_tcb[i] = malloc(sizeof(mthread_tcb_t));
if (new_tcb[i] == NULL) {
mthread_debug("Can't allocate space for tcb");
/* Undo the allocations made so far. */
while (i-- > old_no_threads)
free(new_tcb[i]);
if (old_no_threads > 0) {
new_tcb = realloc(threads, old_no_threads *
sizeof(mthread_tcb_t *));
if (new_tcb == NULL)
mthread_panic("Unable to shrink tcb array");
} else
free(new_tcb);
return(-1);
}
memset(new_tcb[i], '\0', sizeof(mthread_tcb_t)); /* Clear entry */
}
/* We can breath again, let's tell the others about the good news */
/* We can breathe again, let's tell the others about the good news */
threads = new_tcb;
no_threads = new_no_threads;