From f3a7696b190f58379c81bcf8683b86373eedfc63 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 20 Mar 2022 08:30:40 +0100 Subject: Fix allocation success check and deallocation order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libar2simplified_init_context.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libar2simplified_init_context.c b/libar2simplified_init_context.c index c7f8b69..9dcbfb0 100644 --- a/libar2simplified_init_context.c +++ b/libar2simplified_init_context.c @@ -155,9 +155,9 @@ destroy_thread_pool(struct libar2_context *ctx) if (data->threads[i].error) ret = data->threads[i].error; } - free(data->threads); sem_destroy(&data->semaphore); pthread_mutex_destroy(&data->mutex); + free(data->threads); free(data); return ret; } @@ -216,6 +216,10 @@ init_thread_pool(size_t desired, size_t *createdp, struct libar2_context *ctx) size = (desired + 63) / 64; size *= sizeof(uint_least64_t) * 2; data = alignedalloc(1, offsetof(struct user_data, resting), size, ALIGNOF(struct user_data)); + if (!data) { + errno = ENOMEM; + return -1; + } memset(data, 0, offsetof(struct user_data, resting) + size); data->joined = &data->resting[(desired + 63) / 64]; ctx->user_data = data; -- cgit v1.2.3-70-g09d2