Skip to content

Commit a6e42d6

Browse files
committed
Added support for strdup macro replacement on tests. Using auto generated config file.
1 parent 2eb420a commit a6e42d6

15 files changed

+63
-15
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ AC_TYPE_LONG_LONG_INT
6060

6161
# Checks for library functions.
6262
AC_FUNC_FORK
63-
AC_CHECK_FUNCS([gettimeofday memset strstr])
63+
AC_CHECK_FUNCS([gettimeofday memset strstr strdup])
6464

6565
AC_CHECK_PROG([CPPUTEST_HAS_GCC], [gcc], [yes], [no])
6666
AC_CHECK_PROG([CPPUTEST_HAS_CLANG], [clang], [yes], [no])

include/CppUTest/MemoryLeakDetectorMallocMacros.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern "C"
2222
#endif
2323

2424
extern void* cpputest_malloc_location(size_t size, const char* file, int line);
25+
extern char* cpputest_strdup_location(const char* str, const char* file, int line);
2526
extern void* cpputest_calloc_location(size_t count, size_t size, const char* file, int line);
2627
extern void* cpputest_realloc_location(void *, size_t, const char* file, int line);
2728
extern void cpputest_free_location(void* buffer, const char* file, int line);
@@ -34,18 +35,10 @@ extern void crash_on_allocation_number(unsigned number);
3435

3536
#endif
3637

37-
/* NOTE on strdup!
38-
*
39-
* strdup was implemented earlier, however it is *not* an Standard C function but a POSIX function.
40-
* Because of that, it can lead to portability issues by providing more than is available on the local platform.
41-
* For that reason, strdup is *not* implemented as a macro. If you still want to use it, an easy implementation would be:
42-
*
43-
* size_t length = 1 + strlen(str);
44-
* char* result = (char*) cpputest_malloc_location(length, file, line);
45-
* memcpy(result, str, length);
46-
* return result;
47-
*
48-
*/
38+
#ifdef CPPUTEST_HAVE_STRDUP
39+
#define strdup(str) cpputest_strdup_location(str, __FILE__, __LINE__)
40+
#define CPPUTEST_USE_STRDUP_MACROS 1
41+
#endif
4942

5043
#define malloc(a) cpputest_malloc_location(a, __FILE__, __LINE__)
5144
#define calloc(a, b) cpputest_calloc_location(a, b, __FILE__, __LINE__)

include/CppUTest/TestHarness_c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ extern void CHECK_C_LOCATION(int condition, const char* conditionString,
173173
const char* fileName, int lineNumber);
174174

175175
extern void* cpputest_malloc(size_t size);
176+
extern char* cpputest_strdup(const char* str);
176177
extern void* cpputest_calloc(size_t num, size_t size);
177178
extern void* cpputest_realloc(void* ptr, size_t size);
178179
extern void cpputest_free(void* buffer);
179180

180181
extern void* cpputest_malloc_location(size_t size, const char* file, int line);
182+
extern char* cpputest_strdup_location(const char* str, const char* file, int line);
181183
extern void* cpputest_calloc_location(size_t num, size_t size,
182184
const char* file, int line);
183185
extern void* cpputest_realloc_location(void* memory, size_t size,

src/CppUTest/TestHarness_c.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
extern "C"
3535
{
3636

37-
3837
void CHECK_EQUAL_C_BOOL_LOCATION(int expected, int actual, const char* fileName, int lineNumber)
3938
{
4039
UtestShell::getCurrent()->assertEquals(!!expected != !!actual, expected ? "true" : "false", actual ? "true" : "false", NULL, fileName, lineNumber, TestTerminatorWithoutExceptions());
@@ -157,6 +156,11 @@ void* cpputest_malloc(size_t size)
157156
return cpputest_malloc_location(size, "<unknown>", 0);
158157
}
159158

159+
char* cpputest_strdup(const char* str)
160+
{
161+
return cpputest_strdup_location(str, "<unknown>", 0);
162+
}
163+
160164
void* cpputest_calloc(size_t num, size_t size)
161165
{
162166
return cpputest_calloc_location(num, size, "<unknown>", 0);
@@ -193,6 +197,21 @@ void* cpputest_malloc_location(size_t size, const char* file, int line)
193197
return cpputest_malloc_location_with_leak_detection(size, file, line);
194198
}
195199

200+
static size_t strlen(const char * str)
201+
{
202+
size_t n = 0;
203+
while (*str++) n++;
204+
return n;
205+
}
206+
207+
char* cpputest_strdup_location(const char * str, const char* file, int line)
208+
{
209+
size_t length = 1 + strlen(str);
210+
char* result = (char*) cpputest_malloc_location(length, file, line);
211+
PlatformSpecificMemCpy(result, str, length);
212+
return result;
213+
}
214+
196215
void* cpputest_calloc_location(size_t num, size_t size, const char* file, int line)
197216
{
198217
void* mem = cpputest_malloc_location(num * size, file, line);

src/Platforms/C2000/UtestPlatform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#undef free
3535
#undef calloc
3636
#undef realloc
37+
#undef strdup
3738

3839
#define far // eliminate "meaningless type qualifier" warning
3940
#include <time.h>

src/Platforms/Dos/UtestPlatform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#undef free
3535
#undef calloc
3636
#undef realloc
37+
#undef strdup
3738

3839
#define far // eliminate "meaningless type qualifier" warning
3940
#include <time.h>

src/Platforms/Gcc/UtestPlatform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#undef free
3232
#undef calloc
3333
#undef realloc
34+
#undef strdup
3435

3536
#include <sys/time.h>
3637
#include <time.h>

src/Platforms/GccNoStdC/UtestPlatform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#undef free
3131
#undef calloc
3232
#undef realloc
33+
#undef strdup
3334

3435
#include "CppUTest/PlatformSpecificFunctions.h"
3536

src/Platforms/Iar/UtestPlatform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#undef calloc
4040
#undef realloc
4141
#undef free
42+
#undef strdup
4243
#include "CppUTest/PlatformSpecificFunctions.h"
4344

4445
static jmp_buf test_exit_jmp_buf[10];

src/Platforms/Keil/UtestPlatform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#undef free
3232
#undef calloc
3333
#undef realloc
34+
#undef strdup
3435

3536
#define far // eliminate "meaningless type qualifier" warning
3637
#include <time.h>

0 commit comments

Comments
 (0)