From db4ae0ef9dc7d01c8e0c88fcd86981c3c29edbff Mon Sep 17 00:00:00 2001 From: Yves Rutschle Date: Sat, 13 May 2023 22:40:53 +0200 Subject: [PATCH] fix potential memory leak if the second malloc fails --- gap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gap.c b/gap.c index 4156233..3b3f98c 100644 --- a/gap.c +++ b/gap.c @@ -48,7 +48,10 @@ gap_array* gap_init(int len) gap->len = gap_len_alloc(elem_size); if (gap->len < len) gap->len = len; gap->array = malloc(gap->len * elem_size); - if (!gap->array) return NULL; + if (!gap->array) { + free(gap); + return NULL; + } for (int i = 0; i < gap->len; i++) gap->array[i] = NULL;