From 1f8987c6fd8398d7f07da3e5c03604f02368a816 Mon Sep 17 00:00:00 2001 From: pako-23 Date: Wed, 11 Sep 2024 14:19:56 +0200 Subject: [PATCH] wrap strdup in macos to detect leaks --- basic_testing.h | 10 +++++++++- ex/memory_checks.expected | 8 -------- ex/memory_checks/tests/08_fopen_leak.c | 15 --------------- ex/memory_checks/tests/08_tmpfile_leak.c | 15 --------------- ex/memory_checks_cpp.expected | 8 -------- ex/memory_checks_cpp/tests/08_fopen_leak.cc | 15 --------------- ex/memory_checks_cpp/tests/08_tmpfile_leak.cc | 15 --------------- 7 files changed, 9 insertions(+), 77 deletions(-) delete mode 100644 ex/memory_checks/tests/08_fopen_leak.c delete mode 100644 ex/memory_checks/tests/08_tmpfile_leak.c delete mode 100644 ex/memory_checks_cpp/tests/08_fopen_leak.cc delete mode 100644 ex/memory_checks_cpp/tests/08_tmpfile_leak.cc diff --git a/basic_testing.h b/basic_testing.h index 3d53019..d9b7707 100644 --- a/basic_testing.h +++ b/basic_testing.h @@ -888,12 +888,20 @@ void * reallocf (void * ptr, size_t size) { static void *(*libc_reallocf)(void *, size_t) = NULL; if (!libc_reallocf) - libc_reallocf = (void *(*)(size_t, size_t)) dlsym(RTLD_NEXT, "reallocf"); + libc_reallocf = (void *(*)(void *, size_t)) dlsym(RTLD_NEXT, "reallocf"); void * ret = realloc (ptr, size); if (!ret) free (ptr); return ret; } + +BT_POSSIBLY_UNUSED +char * strdup (const char * s) { + size_t len = strlen (s); + char * ret = (char *) malloc (len + 1); + if (ret) strcpy (ret, s); + return ret; +} #endif #ifndef __APPLE__ diff --git a/ex/memory_checks.expected b/ex/memory_checks.expected index d3bf71b..4100ed8 100644 --- a/ex/memory_checks.expected +++ b/ex/memory_checks.expected @@ -81,17 +81,9 @@ Running test 05_byte_budget_allocations_debug... PASS Running test 06_scheduled_failures... PASS Running test 06_scheduled_failures_debug... PASS Running test 07_calloc_reallocarray_failures... PASS -Running test 08_fopen_leak... -FAIL -run 'tests/08_fopen_leak' to see what went wrong -run 'tests/08_fopen_leak -d' with a debugger Running test 08_stdlib_functions... PASS Running test 08_strdup_leak... leaked 8 bytes FAIL run 'tests/08_strdup_leak' to see what went wrong run 'tests/08_strdup_leak -d' with a debugger -Running test 08_tmpfile_leak... -FAIL -run 'tests/08_tmpfile_leak' to see what went wrong -run 'tests/08_tmpfile_leak -d' with a debugger diff --git a/ex/memory_checks/tests/08_fopen_leak.c b/ex/memory_checks/tests/08_fopen_leak.c deleted file mode 100644 index 0bee302..0000000 --- a/ex/memory_checks/tests/08_fopen_leak.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "basic_testing.h" -#include - - - - -TEST (memory_leak) { - FILE * fp = fopen("/tmp/leak_fopen", "w"); - fprintf (fp, "testing\n"); - TEST_PASSED; -} - - - -MAIN_TEST_DRIVER (memory_leak); diff --git a/ex/memory_checks/tests/08_tmpfile_leak.c b/ex/memory_checks/tests/08_tmpfile_leak.c deleted file mode 100644 index c3c2e23..0000000 --- a/ex/memory_checks/tests/08_tmpfile_leak.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "basic_testing.h" -#include - - - -TEST (memory_leak) { - FILE *fp = tmpfile (); - fprintf (fp, "testing\n"); - TEST_PASSED; -} - - - - -MAIN_TEST_DRIVER (memory_leak); diff --git a/ex/memory_checks_cpp.expected b/ex/memory_checks_cpp.expected index 7e163ee..5b25ffa 100644 --- a/ex/memory_checks_cpp.expected +++ b/ex/memory_checks_cpp.expected @@ -169,17 +169,9 @@ Running test 05_byte_budget_allocations_debug... PASS Running test 06_scheduled_failures... PASS Running test 06_scheduled_failures_debug... PASS Running test 07_calloc_reallocarray_failures... PASS -Running test 08_fopen_leak... -FAIL -run 'tests/08_fopen_leak' to see what went wrong -run 'tests/08_fopen_leak -d' with a debugger Running test 08_stdlib_functions... PASS Running test 08_strdup_leak... leaked 8 bytes FAIL run 'tests/08_strdup_leak' to see what went wrong run 'tests/08_strdup_leak -d' with a debugger -Running test 08_tmpfile_leak... -FAIL -run 'tests/08_tmpfile_leak' to see what went wrong -run 'tests/08_tmpfile_leak -d' with a debugger diff --git a/ex/memory_checks_cpp/tests/08_fopen_leak.cc b/ex/memory_checks_cpp/tests/08_fopen_leak.cc deleted file mode 100644 index 0bee302..0000000 --- a/ex/memory_checks_cpp/tests/08_fopen_leak.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "basic_testing.h" -#include - - - - -TEST (memory_leak) { - FILE * fp = fopen("/tmp/leak_fopen", "w"); - fprintf (fp, "testing\n"); - TEST_PASSED; -} - - - -MAIN_TEST_DRIVER (memory_leak); diff --git a/ex/memory_checks_cpp/tests/08_tmpfile_leak.cc b/ex/memory_checks_cpp/tests/08_tmpfile_leak.cc deleted file mode 100644 index c3c2e23..0000000 --- a/ex/memory_checks_cpp/tests/08_tmpfile_leak.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "basic_testing.h" -#include - - - -TEST (memory_leak) { - FILE *fp = tmpfile (); - fprintf (fp, "testing\n"); - TEST_PASSED; -} - - - - -MAIN_TEST_DRIVER (memory_leak);