From ccb29e80c2b2a1451a9788239707d8593f061fb9 Mon Sep 17 00:00:00 2001 From: jsalling Date: Sun, 31 Jan 2016 23:15:00 -0600 Subject: [PATCH] Add NULL safety checks to Fixture allocation functions Consistent use of one-liners and NULL --- extras/fixture/src/unity_fixture.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index b28e30f..348d815 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -179,9 +179,11 @@ void* unity_malloc(size_t size) malloc_fail_countdown--; } + if (size == 0) return NULL; malloc_count++; guard = (Guard*)UNITY_FIXTURE_MALLOC(size + sizeof(Guard) + sizeof(end)); + if (guard == NULL) return NULL; guard->size = size; mem = (char*)&(guard[1]); memcpy(&mem[size], end, sizeof(end)); @@ -227,6 +229,7 @@ void unity_free(void* mem) void* unity_calloc(size_t num, size_t size) { void* mem = unity_malloc(num * size); + if (mem == NULL) return NULL; memset(mem, 0, num*size); return mem; } @@ -237,8 +240,7 @@ void* unity_realloc(void* oldMem, size_t size) // char* memAsChar = (char*)oldMem; void* newMem; - if (oldMem == 0) - return unity_malloc(size); + if (oldMem == NULL) return unity_malloc(size); guard--; if (isOverrun(oldMem)) @@ -250,13 +252,13 @@ void* unity_realloc(void* oldMem, size_t size) if (size == 0) { release_memory(oldMem); - return 0; + return NULL; } - if (guard->size >= size) - return oldMem; + if (guard->size >= size) return oldMem; newMem = unity_malloc(size); + if (newMem == NULL) return NULL; // Do not release old memory memcpy(newMem, oldMem, guard->size); unity_free(oldMem); return newMem; -- GitLab