From befa7d8cd85b9a961f97fdd8c3c430fe0c5a6968 Mon Sep 17 00:00:00 2001 From: "W.C.A. Wijngaards" Date: Fri, 2 Aug 2024 08:54:54 +0200 Subject: [PATCH] - Fix that alloc stats has strdup checks, it stops debuggers from complaining about mismatch at free time. --- config.h.in | 3 +++ configure.ac | 3 +++ doc/Changelog | 4 ++++ util/alloc.c | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/config.h.in b/config.h.in index 4a7143c52..099206025 100644 --- a/config.h.in +++ b/config.h.in @@ -1496,6 +1496,7 @@ struct sockaddr_storage; # define calloc(n,s) unbound_stat_calloc_log(n, s, __FILE__, __LINE__, __func__) # define free(p) unbound_stat_free_log(p, __FILE__, __LINE__, __func__) # define realloc(p,s) unbound_stat_realloc_log(p, s, __FILE__, __LINE__, __func__) +# define strdup(s) unbound_stat_strdup_log(s, __FILE__, __LINE__, __func__) void *unbound_stat_malloc(size_t size); void *unbound_stat_calloc(size_t nmemb, size_t size); void unbound_stat_free(void *ptr); @@ -1508,6 +1509,8 @@ void unbound_stat_free_log(void *ptr, const char* file, int line, const char* func); void *unbound_stat_realloc_log(void *ptr, size_t size, const char* file, int line, const char* func); +char *unbound_stat_strdup_log(const char *s, const char* file, int line, + const char* func); #elif defined(UNBOUND_ALLOC_LITE) # include "util/alloc.h" #endif /* UNBOUND_ALLOC_LITE and UNBOUND_ALLOC_STATS */ diff --git a/configure.ac b/configure.ac index 4d17fb8c1..feeaf34c2 100644 --- a/configure.ac +++ b/configure.ac @@ -2329,6 +2329,7 @@ struct sockaddr_storage; # define calloc(n,s) unbound_stat_calloc_log(n, s, __FILE__, __LINE__, __func__) # define free(p) unbound_stat_free_log(p, __FILE__, __LINE__, __func__) # define realloc(p,s) unbound_stat_realloc_log(p, s, __FILE__, __LINE__, __func__) +# define strdup(s) unbound_stat_strdup_log(s, __FILE__, __LINE__, __func__) void *unbound_stat_malloc(size_t size); void *unbound_stat_calloc(size_t nmemb, size_t size); void unbound_stat_free(void *ptr); @@ -2341,6 +2342,8 @@ void unbound_stat_free_log(void *ptr, const char* file, int line, const char* func); void *unbound_stat_realloc_log(void *ptr, size_t size, const char* file, int line, const char* func); +char *unbound_stat_strdup_log(const char *s, const char* file, int line, + const char* func); #elif defined(UNBOUND_ALLOC_LITE) # include "util/alloc.h" #endif /* UNBOUND_ALLOC_LITE and UNBOUND_ALLOC_STATS */ diff --git a/doc/Changelog b/doc/Changelog index f94ed7fec..a3c4bda09 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,7 @@ +2 August 2024: Wouter + - Fix that alloc stats has strdup checks, it stops debuggers from + complaining about mismatch at free time. + 1 August 2024: Wouter - Fix dnstap test program, cleans up to have clean memory on exit, for tap_data_free, does not delete NULL items. Also it does not try diff --git a/util/alloc.c b/util/alloc.c index 7e9618931..d00976b7f 100644 --- a/util/alloc.c +++ b/util/alloc.c @@ -466,6 +466,19 @@ void *unbound_stat_realloc(void *ptr, size_t size) memcpy(res+8, &mem_special, sizeof(mem_special)); return res+16; } +/** strdup with stats */ +char *unbound_stat_strdup(const char* s) +{ + size_t len; + char* res; + if(!s) return NULL; + len = strlen(s); + res = unbound_stat_malloc(len+1); + if(!res) return NULL; + memmove(res, s, len); + res[len]=0; + return res; +} /** log to file where alloc was done */ void *unbound_stat_malloc_log(size_t size, const char* file, int line, @@ -507,6 +520,15 @@ void *unbound_stat_realloc_log(void *ptr, size_t size, const char* file, return unbound_stat_realloc(ptr, size); } +/** log to file where strdup was done */ +char *unbound_stat_strdup_log(const char *s, const char* file, int line, + const char* func) +{ + log_info("%s:%d %s strdup size %u", file, line, func, + (s?(unsigned)strlen(s)+1:0)); + return unbound_stat_strdup(s); +} + #endif /* UNBOUND_ALLOC_STATS */ #ifdef UNBOUND_ALLOC_LITE #undef malloc