CuTest: add Makefile, remove unsafe string operations

Replace unsafe string operations (strcpy, strcat,
sprintf, vsprintf) with safe equivalents:

1. The one use of strcpy into an allocated buffer was
   replaced with strdup.
2. The one use of strcat was replaced with a call to
   memmove and explicitly setting the NUL terminating
   byte.
3. sprintf()/vsprintf() calls were replaced with calls
   to snprintf()/vsnprintf(), respectively.

Added a Makefile to build the library as, er, a library
and run the test suite.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
This commit is contained in:
Dan Cross 2018-10-11 10:51:05 +00:00 committed by Andrew Pamment
parent 5879cc6f49
commit 33beceadd3
3 changed files with 29 additions and 13 deletions

View File

@ -19,10 +19,7 @@ char* CuStrAlloc(int size)
char* CuStrCopy(const char* old) char* CuStrCopy(const char* old)
{ {
int len = strlen(old); return strdup(old);
char* newStr = CuStrAlloc(len + 1);
strcpy(newStr, old);
return newStr;
} }
/*-------------------------------------------------------------------------* /*-------------------------------------------------------------------------*
@ -71,8 +68,9 @@ void CuStringAppend(CuString* str, const char* text)
length = strlen(text); length = strlen(text);
if (str->length + length + 1 >= str->size) if (str->length + length + 1 >= str->size)
CuStringResize(str, str->length + length + 1 + STRING_INC); CuStringResize(str, str->length + length + 1 + STRING_INC);
memmove(str->buffer + str->length, text, length);
str->length += length; str->length += length;
strcat(str->buffer, text); str->buffer[str->length] = '\0';
} }
void CuStringAppendChar(CuString* str, char ch) void CuStringAppendChar(CuString* str, char ch)
@ -88,7 +86,7 @@ void CuStringAppendFormat(CuString* str, const char* format, ...)
va_list argp; va_list argp;
char buf[HUGE_STRING_LEN]; char buf[HUGE_STRING_LEN];
va_start(argp, format); va_start(argp, format);
vsprintf(buf, format, argp); vsnprintf(buf, sizeof buf, format, argp);
va_end(argp); va_end(argp);
CuStringAppend(str, buf); CuStringAppend(str, buf);
} }
@ -149,7 +147,7 @@ static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* str
{ {
char buf[HUGE_STRING_LEN]; char buf[HUGE_STRING_LEN];
sprintf(buf, "%s:%d: ", file, line); snprintf(buf, sizeof buf, "%s:%d: ", file, line);
CuStringInsert(string, buf, 0); CuStringInsert(string, buf, 0);
tc->failed = 1; tc->failed = 1;
@ -207,7 +205,7 @@ void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const cha
{ {
char buf[STRING_MAX]; char buf[STRING_MAX];
if (expected == actual) return; if (expected == actual) return;
sprintf(buf, "expected <%d> but was <%d>", expected, actual); snprintf(buf, sizeof buf, "expected <%d> but was <%d>", expected, actual);
CuFail_Line(tc, file, line, message, buf); CuFail_Line(tc, file, line, message, buf);
} }
@ -216,7 +214,7 @@ void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const cha
{ {
char buf[STRING_MAX]; char buf[STRING_MAX];
if (fabs(expected - actual) <= delta) return; if (fabs(expected - actual) <= delta) return;
sprintf(buf, "expected <%f> but was <%f>", expected, actual); snprintf(buf, sizeof buf, "expected <%f> but was <%f>", expected, actual);
CuFail_Line(tc, file, line, message, buf); CuFail_Line(tc, file, line, message, buf);
} }
@ -226,7 +224,7 @@ void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const cha
{ {
char buf[STRING_MAX]; char buf[STRING_MAX];
if (expected == actual) return; if (expected == actual) return;
sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual); snprintf(buf, sizeof buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
CuFail_Line(tc, file, line, message, buf); CuFail_Line(tc, file, line, message, buf);
} }

View File

@ -213,7 +213,8 @@ void TestCuAssertPtrEquals_Failure(CuTest* tc)
CuTestInit(&tc2, "MyTest", TestPasses); CuTestInit(&tc2, "MyTest", TestPasses);
/* test failing case */ /* test failing case */
sprintf(expected_message, "expected pointer <0x%p> but was <0x%p>", nullPtr, &x); snprintf(expected_message, sizeof expected_message,
"expected pointer <0x%p> but was <0x%p>", nullPtr, &x);
CuAssertPtrEquals(&tc2, NULL, &x); CuAssertPtrEquals(&tc2, NULL, &x);
CuAssertTrue(tc, tc2.failed); CuAssertTrue(tc, tc2.failed);
CompareAsserts(tc, "CuAssertPtrEquals failed", expected_message, tc2.message); CompareAsserts(tc, "CuAssertPtrEquals failed", expected_message, tc2.message);
@ -638,8 +639,9 @@ void TestAssertDblEquals(CuTest* tc)
CuTest *tc2 = CuTestNew("TestAssertDblEquals", zTestFails); CuTest *tc2 = CuTestNew("TestAssertDblEquals", zTestFails);
char expected[STRING_MAX]; char expected[STRING_MAX];
char expectedMsg[STRING_MAX]; char expectedMsg[STRING_MAX];
sprintf(expected, "expected <%lf> but was <%lf>", x, y); snprintf(expected, sizeof expected, "expected <%lf> but was <%lf>", x, y);
sprintf(expectedMsg, "some text: expected <%lf> but was <%lf>", x, y); snprintf(expectedMsg, sizeof expectedMsg,
"some text: expected <%lf> but was <%lf>", x, y);
CuTestInit(tc2, "TestAssertDblEquals", TestPasses); CuTestInit(tc2, "TestAssertDblEquals", TestPasses);

16
deps/cutest-1.5/Makefile vendored Normal file
View File

@ -0,0 +1,16 @@
LIB= libcutest.a
OBJS= CuTest.o
$(LIB): $(OBJS)
ar ru $(LIB) $(OBJS)
ranlib $(LIB)
$(OBJS): CuTest.h
test: $(LIB)
$(CC) -o AllTests AllTests.c CuTestTest.c $(LIB)
./AllTests
clean:
rm -f AllTests *.o $(LIB)