From 8e3b4411ed0d20990e907d74f32cc68e6a3c72be Mon Sep 17 00:00:00 2001 From: cozis Date: Tue, 19 Apr 2022 01:30:32 +0200 Subject: [PATCH] fixed allocator use after free and added some somments --- xjson.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/xjson.c b/xjson.c index 753898b..6d19887 100644 --- a/xjson.c +++ b/xjson.c @@ -53,9 +53,11 @@ xj_alloc *xj_alloc_using(void *mem, int size, int ext, void (*free)(void*)) void xj_alloc_del(xj_alloc *alloc) { - if(alloc->free != NULL) - alloc->free(alloc); - + // Free all of the allocator's chunks, + // with exception of the first one, + // which is allocated with the allocator's + // header and must be deallocated with + // the user-provided callback- chunk_t *curr = alloc->tail; while(curr->prev != NULL) { @@ -63,6 +65,11 @@ void xj_alloc_del(xj_alloc *alloc) free(curr); curr = prev; } + + // Free the allocator header and first + // chunk. + if(alloc->free != NULL) + alloc->free(alloc); } unsigned long long next_aligned(unsigned long long n)