From 1affe544d2a0698d5c7d84f9bd9e6db587613e32 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 6 Oct 2015 10:44:44 +0200 Subject: [PATCH] unity_fixture: Make unity_free() NULL-safe At the start of unity_free(), check mem for NULL, and return immediately if it is, so we don't crash in this case. This mimics the behaviour of most free() implementations. Closes #135. Signed-off-by: Gergely Nagy --- extras/fixture/src/unity_fixture.c | 9 ++++++++- extras/fixture/test/unity_fixture_Test.c | 5 +++++ extras/fixture/test/unity_fixture_TestRunner.c | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index 6fb1ede..7cafaa3 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -227,7 +227,14 @@ static void release_memory(void * mem) void unity_free(void * mem) { - int overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0; + int overrun; + + if (mem == NULL) + { + return; + } + + overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0; release_memory(mem); if (overrun) { diff --git a/extras/fixture/test/unity_fixture_Test.c b/extras/fixture/test/unity_fixture_Test.c index 1f209e9..27652f6 100644 --- a/extras/fixture/test/unity_fixture_Test.c +++ b/extras/fixture/test/unity_fixture_Test.c @@ -132,6 +132,11 @@ TEST(UnityFixture, PointerSet) TEST_ASSERT_POINTERS_EQUAL(&c2, p2); } +TEST(UnityFixture, FreeNULLSafety) +{ + unity_free(NULL); +} + //------------------------------------------------------------ TEST_GROUP(UnityCommandOptions); diff --git a/extras/fixture/test/unity_fixture_TestRunner.c b/extras/fixture/test/unity_fixture_TestRunner.c index 6fb0602..28d8e56 100644 --- a/extras/fixture/test/unity_fixture_TestRunner.c +++ b/extras/fixture/test/unity_fixture_TestRunner.c @@ -18,6 +18,7 @@ TEST_GROUP_RUNNER(UnityFixture) RUN_TEST_CASE(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer); RUN_TEST_CASE(UnityFixture, CallocFillsWithZero); RUN_TEST_CASE(UnityFixture, PointerSet); + RUN_TEST_CASE(UnityFixture, FreeNULLSafety); } TEST_GROUP_RUNNER(UnityCommandOptions) -- GitLab