From 094c05e904c0f8c97cbc16350ffaa4167eb5d3ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bryon=20Gloden=2C=20CISSP=C2=AE?= Date: Tue, 26 Jul 2016 14:40:00 -0400 Subject: [PATCH 1/2] Update unity_fixture_Test.c [../Unity-master/extras/fixture/test/unity_fixture_Test.c:530]: (error) Deallocating a deallocated pointer: n1 This solution from Daniel Fischer was helpful in fixing the error because if realloc returns a pointer to a different location, the old location is freed. Found by https://github.com/bryongloden/cppcheck --- extras/fixture/test/unity_fixture_Test.c | 1 - 1 file changed, 1 deletion(-) diff --git a/extras/fixture/test/unity_fixture_Test.c b/extras/fixture/test/unity_fixture_Test.c index 3fee98f..8f3b885 100644 --- a/extras/fixture/test/unity_fixture_Test.c +++ b/extras/fixture/test/unity_fixture_Test.c @@ -527,7 +527,6 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem) TEST_ASSERT_NULL(out_of_mem); TEST_ASSERT_NOT_EQUAL(n2, n1); free(n2); - free(n1); free(m); #endif } From 13160e5f1e5462e36adc539b594c052bacf1687c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bryon=20Gloden=2C=20CISSP=C2=AE?= Date: Thu, 28 Jul 2016 17:11:11 -0400 Subject: [PATCH 2/2] Update unity_fixture_Test.c I believe if realloc() returns a pointer to a different location, the old location is freed. However, the pointer 'n1' is not freed if realloc fails to obtain a large enough block of memory and returns NULL. (more details on [StackOverflow](http://stackoverflow.com/a/16676964)). --- extras/fixture/test/unity_fixture_Test.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/extras/fixture/test/unity_fixture_Test.c b/extras/fixture/test/unity_fixture_Test.c index 8f3b885..0729e1f 100644 --- a/extras/fixture/test/unity_fixture_Test.c +++ b/extras/fixture/test/unity_fixture_Test.c @@ -524,7 +524,11 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem) void* out_of_mem = realloc(n1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1); void* n2 = malloc(10); TEST_ASSERT_NOT_NULL(m); - TEST_ASSERT_NULL(out_of_mem); + if (out_of_mem == NULL) + { + free(n1); + TEST_ASSERT_NULL(out_of_mem); + } TEST_ASSERT_NOT_EQUAL(n2, n1); free(n2); free(m);