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

@ -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");

View File

@ -4,7 +4,7 @@
#include <setjmp.h>
#include <stdarg.h>
#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;
};

View File

@ -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)

View File

@ -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