From 0fe17b4c0f03aefccef555af66c00839872ea1f5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 27 Feb 2016 12:25:49 +0100 Subject: [PATCH] fix memory leak in CuTest when a test fails --- CuTest.c | 9 ++++++--- CuTest.h | 4 ++-- CuTestTest.c | 22 +++++++++++----------- Makefile | 2 +- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/CuTest.c b/CuTest.c index 8f61199..da6d180 100644 --- a/CuTest.c +++ b/CuTest.c @@ -114,7 +114,7 @@ void CuTestInit(CuTest* t, const char* name, TestFunction function) t->name = CuStrCopy(name); t->failed = 0; t->ran = 0; - t->message = NULL; + t->message = NULL; t->function = function; t->jumpBuf = NULL; } @@ -129,6 +129,7 @@ CuTest* CuTestNew(const char* name, TestFunction function) void CuTestDelete(CuTest *t) { if (!t) return; + CuStringDelete(t->message); free(t->name); free(t); } @@ -153,7 +154,9 @@ static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* str CuStringInsert(string, buf, 0); tc->failed = 1; - tc->message = string->buffer; + free(tc->message); + tc->message = CuStringNew(); + CuStringAppend(tc->message, string->buffer); if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0); } @@ -327,7 +330,7 @@ void CuSuiteDetails(CuSuite* testSuite, CuString* details) { failCount++; CuStringAppendFormat(details, "%d) %s: %s\n", - failCount, testCase->name, testCase->message); + failCount, testCase->name, testCase->message->buffer); } } CuStringAppend(details, "\n!!!FAILURES!!!\n"); diff --git a/CuTest.h b/CuTest.h index 3866107..4d08be3 100644 --- a/CuTest.h +++ b/CuTest.h @@ -4,7 +4,7 @@ #include #include -#define CUTEST_VERSION "CuTest 1.5" +#define CUTEST_VERSION "CuTest 1.5b" /* CuString */ @@ -46,7 +46,7 @@ struct CuTest TestFunction function; int failed; int ran; - const char* message; + CuString *message; jmp_buf *jumpBuf; }; diff --git a/CuTestTest.c b/CuTestTest.c index 8e90768..1911131 100644 --- a/CuTestTest.c +++ b/CuTestTest.c @@ -12,17 +12,17 @@ #define CompareAsserts(tc, message, expected, actual) X_CompareAsserts((tc), __FILE__, __LINE__, (message), (expected), (actual)) -static void X_CompareAsserts(CuTest* tc, const char *file, int line, const char* message, const char* expected, const char* actual) +static void X_CompareAsserts(CuTest* tc, const char *file, int line, const char* message, const char* expected, CuString *actual) { int mismatch; - if (expected == NULL || actual == NULL) { + if (expected == NULL || actual == NULL || actual==NULL) { mismatch = (expected != NULL || actual != NULL); } else { const char *front = __FILE__ ":"; const size_t frontLen = strlen(front); const size_t expectedLen = strlen(expected); - const char *matchStr = actual; + const char *matchStr = actual->buffer; mismatch = (strncmp(matchStr, front, frontLen) != 0); if (!mismatch) { @@ -148,7 +148,7 @@ void TestCuTestNew(CuTest* tc) CuTest* tc2 = CuTestNew("MyTest", TestPasses); CuAssertStrEquals(tc, "MyTest", tc2->name); CuAssertTrue(tc, !tc2->failed); - CuAssertTrue(tc, tc2->message == NULL); + CuAssertTrue(tc, tc2->message == NULL); CuAssertTrue(tc, tc2->function == TestPasses); CuAssertTrue(tc, tc2->ran == 0); CuAssertTrue(tc, tc2->jumpBuf == NULL); @@ -161,7 +161,7 @@ void TestCuTestInit(CuTest *tc) CuTestInit(&tc2, "MyTest", TestPasses); CuAssertStrEquals(tc, "MyTest", tc2.name); CuAssertTrue(tc, !tc2.failed); - CuAssertTrue(tc, tc2.message == NULL); + CuAssertTrue(tc, tc2.message == NULL); CuAssertTrue(tc, tc2.function == TestPasses); CuAssertTrue(tc, tc2.ran == 0); CuAssertTrue(tc, tc2.jumpBuf == NULL); @@ -174,7 +174,7 @@ void TestCuAssert(CuTest* tc) CuAssert(&tc2, "test 1", 5 == 4 + 1); CuAssertTrue(tc, !tc2.failed); - CuAssertTrue(tc, tc2.message == NULL); + CuAssertTrue(tc, tc2.message == NULL); CuAssert(&tc2, "test 2", 0); CuAssertTrue(tc, tc2.failed); @@ -200,7 +200,7 @@ void TestCuAssertPtrEquals_Success(CuTest* tc) /* test success case */ CuAssertPtrEquals(&tc2, &x, &x); CuAssertTrue(tc, ! tc2.failed); - CuAssertTrue(tc, NULL == tc2.message); + CuAssertTrue(tc, tc2.message == NULL); } void TestCuAssertPtrEquals_Failure(CuTest* tc) @@ -229,7 +229,7 @@ void TestCuAssertPtrNotNull_Success(CuTest* tc) /* test success case */ CuAssertPtrNotNull(&tc2, &x); CuAssertTrue(tc, ! tc2.failed); - CuAssertTrue(tc, NULL == tc2.message); + CuAssertTrue(tc, tc2.message == NULL); } void TestCuAssertPtrNotNull_Failure(CuTest* tc) @@ -645,11 +645,11 @@ void TestAssertDblEquals(CuTest* tc) CuAssertDblEquals(tc2, x, x, 0.0); CuAssertTrue(tc, ! tc2->failed); - CuAssertTrue(tc, NULL == tc2->message); - + CuAssertTrue(tc, tc2->message == NULL); + CuAssertDblEquals(tc2, x, y, 0.01); CuAssertTrue(tc, ! tc2->failed); - CuAssertTrue(tc, NULL == tc2->message); + CuAssertTrue(tc, tc2->message == NULL); tc2->jumpBuf = &buf; if (setjmp(buf) == 0) diff --git a/Makefile b/Makefile index eab0b66..8786bf7 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ test: bin/CuTestTest @bin/CuTestTest bin/CuTestTest: AllTests.c CuTestTest.c CuTest.c | bin - $(CC) $(CFLAGS) $(INCLUDES) -lm -o $@ $^ + $(CC) $(CFLAGS) $(INCLUDES) -Wno-address -lm -o $@ $^ clean: @rm -rf *~ bin