fix memory leak in CuTest when a test fails

This commit is contained in:
Enno Rehling 2016-02-27 12:25:49 +01:00
parent 6e268687db
commit 0fe17b4c0f
4 changed files with 20 additions and 17 deletions

View File

@ -129,6 +129,7 @@ CuTest* CuTestNew(const char* name, TestFunction function)
void CuTestDelete(CuTest *t) void CuTestDelete(CuTest *t)
{ {
if (!t) return; if (!t) return;
CuStringDelete(t->message);
free(t->name); free(t->name);
free(t); free(t);
} }
@ -153,7 +154,9 @@ static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* str
CuStringInsert(string, buf, 0); CuStringInsert(string, buf, 0);
tc->failed = 1; 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); if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
} }
@ -327,7 +330,7 @@ void CuSuiteDetails(CuSuite* testSuite, CuString* details)
{ {
failCount++; failCount++;
CuStringAppendFormat(details, "%d) %s: %s\n", CuStringAppendFormat(details, "%d) %s: %s\n",
failCount, testCase->name, testCase->message); failCount, testCase->name, testCase->message->buffer);
} }
} }
CuStringAppend(details, "\n!!!FAILURES!!!\n"); CuStringAppend(details, "\n!!!FAILURES!!!\n");

View File

@ -4,7 +4,7 @@
#include <setjmp.h> #include <setjmp.h>
#include <stdarg.h> #include <stdarg.h>
#define CUTEST_VERSION "CuTest 1.5" #define CUTEST_VERSION "CuTest 1.5b"
/* CuString */ /* CuString */
@ -46,7 +46,7 @@ struct CuTest
TestFunction function; TestFunction function;
int failed; int failed;
int ran; int ran;
const char* message; CuString *message;
jmp_buf *jumpBuf; jmp_buf *jumpBuf;
}; };

View File

@ -12,17 +12,17 @@
#define CompareAsserts(tc, message, expected, actual) X_CompareAsserts((tc), __FILE__, __LINE__, (message), (expected), (actual)) #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; int mismatch;
if (expected == NULL || actual == NULL) { if (expected == NULL || actual == NULL || actual==NULL) {
mismatch = (expected != NULL || actual != NULL); mismatch = (expected != NULL || actual != NULL);
} else { } else {
const char *front = __FILE__ ":"; const char *front = __FILE__ ":";
const size_t frontLen = strlen(front); const size_t frontLen = strlen(front);
const size_t expectedLen = strlen(expected); const size_t expectedLen = strlen(expected);
const char *matchStr = actual; const char *matchStr = actual->buffer;
mismatch = (strncmp(matchStr, front, frontLen) != 0); mismatch = (strncmp(matchStr, front, frontLen) != 0);
if (!mismatch) { if (!mismatch) {
@ -200,7 +200,7 @@ void TestCuAssertPtrEquals_Success(CuTest* tc)
/* test success case */ /* test success case */
CuAssertPtrEquals(&tc2, &x, &x); CuAssertPtrEquals(&tc2, &x, &x);
CuAssertTrue(tc, ! tc2.failed); CuAssertTrue(tc, ! tc2.failed);
CuAssertTrue(tc, NULL == tc2.message); CuAssertTrue(tc, tc2.message == NULL);
} }
void TestCuAssertPtrEquals_Failure(CuTest* tc) void TestCuAssertPtrEquals_Failure(CuTest* tc)
@ -229,7 +229,7 @@ void TestCuAssertPtrNotNull_Success(CuTest* tc)
/* test success case */ /* test success case */
CuAssertPtrNotNull(&tc2, &x); CuAssertPtrNotNull(&tc2, &x);
CuAssertTrue(tc, ! tc2.failed); CuAssertTrue(tc, ! tc2.failed);
CuAssertTrue(tc, NULL == tc2.message); CuAssertTrue(tc, tc2.message == NULL);
} }
void TestCuAssertPtrNotNull_Failure(CuTest* tc) void TestCuAssertPtrNotNull_Failure(CuTest* tc)
@ -645,11 +645,11 @@ void TestAssertDblEquals(CuTest* tc)
CuAssertDblEquals(tc2, x, x, 0.0); CuAssertDblEquals(tc2, x, x, 0.0);
CuAssertTrue(tc, ! tc2->failed); CuAssertTrue(tc, ! tc2->failed);
CuAssertTrue(tc, NULL == tc2->message); CuAssertTrue(tc, tc2->message == NULL);
CuAssertDblEquals(tc2, x, y, 0.01); CuAssertDblEquals(tc2, x, y, 0.01);
CuAssertTrue(tc, ! tc2->failed); CuAssertTrue(tc, ! tc2->failed);
CuAssertTrue(tc, NULL == tc2->message); CuAssertTrue(tc, tc2->message == NULL);
tc2->jumpBuf = &buf; tc2->jumpBuf = &buf;
if (setjmp(buf) == 0) if (setjmp(buf) == 0)

View File

@ -12,7 +12,7 @@ test: bin/CuTestTest
@bin/CuTestTest @bin/CuTestTest
bin/CuTestTest: AllTests.c CuTestTest.c CuTest.c | bin bin/CuTestTest: AllTests.c CuTestTest.c CuTest.c | bin
$(CC) $(CFLAGS) $(INCLUDES) -lm -o $@ $^ $(CC) $(CFLAGS) $(INCLUDES) -Wno-address -lm -o $@ $^
clean: clean:
@rm -rf *~ bin @rm -rf *~ bin