未验证 提交 1affe544 编写于 作者: G Gergely Nagy

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: NGergely Nagy <algernon@madhouse-project.org>
上级 7f9f937c
...@@ -227,7 +227,14 @@ static void release_memory(void * mem) ...@@ -227,7 +227,14 @@ static void release_memory(void * mem)
void unity_free(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); release_memory(mem);
if (overrun) if (overrun)
{ {
......
...@@ -132,6 +132,11 @@ TEST(UnityFixture, PointerSet) ...@@ -132,6 +132,11 @@ TEST(UnityFixture, PointerSet)
TEST_ASSERT_POINTERS_EQUAL(&c2, p2); TEST_ASSERT_POINTERS_EQUAL(&c2, p2);
} }
TEST(UnityFixture, FreeNULLSafety)
{
unity_free(NULL);
}
//------------------------------------------------------------ //------------------------------------------------------------
TEST_GROUP(UnityCommandOptions); TEST_GROUP(UnityCommandOptions);
......
...@@ -18,6 +18,7 @@ TEST_GROUP_RUNNER(UnityFixture) ...@@ -18,6 +18,7 @@ TEST_GROUP_RUNNER(UnityFixture)
RUN_TEST_CASE(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer); RUN_TEST_CASE(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer);
RUN_TEST_CASE(UnityFixture, CallocFillsWithZero); RUN_TEST_CASE(UnityFixture, CallocFillsWithZero);
RUN_TEST_CASE(UnityFixture, PointerSet); RUN_TEST_CASE(UnityFixture, PointerSet);
RUN_TEST_CASE(UnityFixture, FreeNULLSafety);
} }
TEST_GROUP_RUNNER(UnityCommandOptions) TEST_GROUP_RUNNER(UnityCommandOptions)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册