From 75e88a9bc47ec47646e9a26b3aa6c5b20a4c38bf Mon Sep 17 00:00:00 2001 From: Alessio Centazzo Date: Sun, 29 Sep 2019 21:45:51 -0700 Subject: [PATCH] Fix MallocThenReallocGrowsMemoryInPlace The realloc was not taking in account extra bytes needed for the the pointer proper alignment --- extras/fixture/src/unity_fixture.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index 4a064c3..7272a8a 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -317,11 +317,15 @@ void* unity_realloc(void* oldMem, size_t size) if (guard->size >= size) return oldMem; #ifdef UNITY_EXCLUDE_STDLIB_MALLOC /* Optimization if memory is expandable */ - if (oldMem == unity_heap + heap_index - guard->size - sizeof(end) && - heap_index + size - guard->size <= UNITY_INTERNAL_HEAP_SIZE_BYTES) { - release_memory(oldMem); /* Not thread-safe, like unity_heap generally */ - return unity_malloc(size); /* No memcpy since data is in place */ + size_t old_total_size = unity_size_round_up(guard->size + sizeof(end)); + + if ((oldMem == unity_heap + heap_index - old_total_size) && + ((heap_index - old_total_size + unity_size_round_up(size + sizeof(end))) <= UNITY_INTERNAL_HEAP_SIZE_BYTES)) + { + release_memory(oldMem); /* Not thread-safe, like unity_heap generally */ + return unity_malloc(size); /* No memcpy since data is in place */ + } } #endif newMem = unity_malloc(size); -- GitLab