From 92f6d5dd08c26ae8c9a681e8058561a5923e762b Mon Sep 17 00:00:00 2001 From: jsalling Date: Sun, 21 Aug 2016 11:53:15 -0500 Subject: [PATCH] Verify the tests for Internal Malloc implementation free all the heap Make it more clear that each test of the internal heap implementation should free in LIFO order. Without this check, memory can be stranded but still pass. --- extras/fixture/test/unity_fixture_Test.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/extras/fixture/test/unity_fixture_Test.c b/extras/fixture/test/unity_fixture_Test.c index 472ffb1..3f2d32f 100644 --- a/extras/fixture/test/unity_fixture_Test.c +++ b/extras/fixture/test/unity_fixture_Test.c @@ -479,6 +479,10 @@ TEST(LeakDetection, PointerSettingMax) //------------------------------------------------------------ TEST_GROUP(InternalMalloc); +#define TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(first_mem_ptr, ptr) \ + ptr = malloc(10); free(ptr); \ + TEST_ASSERT_EQUAL_PTR_MESSAGE(first_mem_ptr, ptr, "Memory was stranded, free in LIFO order"); + TEST_SETUP(InternalMalloc) { } TEST_TEAR_DOWN(InternalMalloc) { } @@ -491,6 +495,7 @@ TEST(InternalMalloc, MallocPastBufferFails) free(m); TEST_ASSERT_NOT_NULL(m); TEST_ASSERT_NULL(n); + TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n); #endif } @@ -502,6 +507,7 @@ TEST(InternalMalloc, CallocPastBufferFails) free(m); TEST_ASSERT_NOT_NULL(m); TEST_ASSERT_NULL(n); + TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n); #endif } @@ -513,6 +519,7 @@ TEST(InternalMalloc, MallocThenReallocGrowsMemoryInPlace) free(n); TEST_ASSERT_NOT_NULL(m); TEST_ASSERT_EQUAL(m, n); + TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n); #endif } @@ -523,6 +530,7 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem) void* n1 = malloc(10); void* out_of_mem = realloc(n1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1); void* n2 = malloc(10); + free(n2); if (out_of_mem == NULL) free(n1); free(m); @@ -530,5 +538,6 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem) TEST_ASSERT_NOT_NULL(m); // Got a real memory location TEST_ASSERT_NULL(out_of_mem); // The realloc should have failed TEST_ASSERT_NOT_EQUAL(n2, n1); // If n1 != n2 then realloc did not free n1 + TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n2); #endif } -- GitLab