From fc148563217e7bdd055668ac4376289a3ffa3303 Mon Sep 17 00:00:00 2001 From: Alessio Centazzo Date: Sun, 29 Sep 2019 15:08:48 -0700 Subject: [PATCH] Fix malloc alignment Make sure the malloc alignment is always aligned to the architecture's pointer size --- extras/fixture/src/unity_fixture.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index 67807e1..4a064c3 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -188,13 +188,26 @@ typedef struct GuardBytes } Guard; +#define UNITY_MALLOC_ALIGNMENT (UNITY_POINTER_WIDTH / 8) static const char end[] = "END"; + +static size_t unity_size_round_up(size_t size) +{ + size_t rounded_size; + + rounded_size = ((size + UNITY_MALLOC_ALIGNMENT - 1) / UNITY_MALLOC_ALIGNMENT) * UNITY_MALLOC_ALIGNMENT; + + return rounded_size; +} + void* unity_malloc(size_t size) { char* mem; Guard* guard; - size_t total_size = size + sizeof(Guard) + sizeof(end); + size_t total_size; + + total_size = sizeof(Guard) + unity_size_round_up(size + sizeof(end)); if (malloc_fail_countdown != MALLOC_DONT_FAIL) { @@ -243,9 +256,13 @@ static void release_memory(void* mem) malloc_count--; #ifdef UNITY_EXCLUDE_STDLIB_MALLOC - if (mem == unity_heap + heap_index - guard->size - sizeof(end)) + size_t block_size; + + block_size = unity_size_round_up(guard->size + sizeof(end)); + + if (mem == unity_heap + heap_index - block_size) { - heap_index -= (guard->size + sizeof(Guard) + sizeof(end)); + heap_index -= (sizeof(Guard) + block_size); } #else UNITY_FIXTURE_FREE(guard);