From 0653a9e48aede5e237ac9d84280344668b10a873 Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" <mlp@thesmartpolitenerd.com> Date: Sun, 2 Aug 2015 14:34:45 +0200 Subject: [PATCH] add explicit null checks to system_allocator malloc/realloc --- src/system_allocator.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/system_allocator.c b/src/system_allocator.c index b34810fa..f9dd291a 100644 --- a/src/system_allocator.c +++ b/src/system_allocator.c @@ -7,6 +7,9 @@ static void* system_alloc(HAllocator *allocator, size_t size) { void* ptr = malloc(size + sizeof(size_t)); + if (!ptr) { + return NULL; + } #ifdef DEBUG__MEMFILL memset(ptr, DEBUG__MEMFILL, size + sizeof(size_t)); #endif @@ -15,9 +18,13 @@ static void* system_alloc(HAllocator *allocator, size_t size) { } static void* system_realloc(HAllocator *allocator, void* ptr, size_t size) { - if (ptr == NULL) + if (!ptr) { return system_alloc(allocator, size); + } ptr = realloc(ptr - sizeof(size_t), size + sizeof(size_t)); + if (!ptr) { + return NULL; + } *(size_t*)ptr = size; #ifdef DEBUG__MEMFILL size_t old_size = *(size_t*)ptr; @@ -28,8 +35,9 @@ static void* system_realloc(HAllocator *allocator, void* ptr, size_t size) { } static void system_free(HAllocator *allocator, void* ptr) { - if (ptr != NULL) + if (ptr) { free(ptr - sizeof(size_t)); + } } HAllocator system_allocator = { -- GitLab