mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-06-25 18:57:58 +08:00
Add NULL safety checks to Fixture allocation functions
Consistent use of one-liners and NULL
This commit is contained in:
@ -179,9 +179,11 @@ void* unity_malloc(size_t size)
|
|||||||
malloc_fail_countdown--;
|
malloc_fail_countdown--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (size == 0) return NULL;
|
||||||
malloc_count++;
|
malloc_count++;
|
||||||
|
|
||||||
guard = (Guard*)UNITY_FIXTURE_MALLOC(size + sizeof(Guard) + sizeof(end));
|
guard = (Guard*)UNITY_FIXTURE_MALLOC(size + sizeof(Guard) + sizeof(end));
|
||||||
|
if (guard == NULL) return NULL;
|
||||||
guard->size = size;
|
guard->size = size;
|
||||||
mem = (char*)&(guard[1]);
|
mem = (char*)&(guard[1]);
|
||||||
memcpy(&mem[size], end, sizeof(end));
|
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* unity_calloc(size_t num, size_t size)
|
||||||
{
|
{
|
||||||
void* mem = unity_malloc(num * size);
|
void* mem = unity_malloc(num * size);
|
||||||
|
if (mem == NULL) return NULL;
|
||||||
memset(mem, 0, num*size);
|
memset(mem, 0, num*size);
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
@ -237,8 +240,7 @@ void* unity_realloc(void* oldMem, size_t size)
|
|||||||
// char* memAsChar = (char*)oldMem;
|
// char* memAsChar = (char*)oldMem;
|
||||||
void* newMem;
|
void* newMem;
|
||||||
|
|
||||||
if (oldMem == 0)
|
if (oldMem == NULL) return unity_malloc(size);
|
||||||
return unity_malloc(size);
|
|
||||||
|
|
||||||
guard--;
|
guard--;
|
||||||
if (isOverrun(oldMem))
|
if (isOverrun(oldMem))
|
||||||
@ -250,13 +252,13 @@ void* unity_realloc(void* oldMem, size_t size)
|
|||||||
if (size == 0)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
release_memory(oldMem);
|
release_memory(oldMem);
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (guard->size >= size)
|
if (guard->size >= size) return oldMem;
|
||||||
return oldMem;
|
|
||||||
|
|
||||||
newMem = unity_malloc(size);
|
newMem = unity_malloc(size);
|
||||||
|
if (newMem == NULL) return NULL; // Do not release old memory
|
||||||
memcpy(newMem, oldMem, guard->size);
|
memcpy(newMem, oldMem, guard->size);
|
||||||
unity_free(oldMem);
|
unity_free(oldMem);
|
||||||
return newMem;
|
return newMem;
|
||||||
|
Reference in New Issue
Block a user