From 80acb76f461c77a0ec7f3fc9a7a0817724831dc3 Mon Sep 17 00:00:00 2001 From: Simon Kelley Date: Sat, 24 Jan 2026 23:59:06 +0000 Subject: [PATCH] Don't start malloc() logging until the log system is configured. --- src/dnsmasq.c | 3 +++ src/dnsmasq.h | 1 + src/util.c | 15 +++++++++------ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/dnsmasq.c b/src/dnsmasq.c index 31dd1e3..f6daae6 100644 --- a/src/dnsmasq.c +++ b/src/dnsmasq.c @@ -864,6 +864,9 @@ int main (int argc, char **argv) } #endif + /* Don't start logging malloc before logging is set up. */ + daemon->log_malloc = option_bool(OPT_LOG_MALLOC); + if (daemon->port == 0) my_syslog(LOG_INFO, _("started, version %s DNS disabled"), VERSION); else diff --git a/src/dnsmasq.h b/src/dnsmasq.h index 80e9304..b9062e3 100644 --- a/src/dnsmasq.h +++ b/src/dnsmasq.h @@ -1208,6 +1208,7 @@ extern struct daemon { int log_fac; /* log facility */ char *log_file; /* optional log file */ int max_logs; /* queue limit */ + int log_malloc; /* log malloc/realloc/free */ int randport_limit; /* Maximum number of source ports for query. */ int cachesize, ftabsize; int port, query_port, min_port, max_port; diff --git a/src/util.c b/src/util.c index 1f925e7..b5c961b 100644 --- a/src/util.c +++ b/src/util.c @@ -938,7 +938,7 @@ void *whine_malloc_real(const char *func, unsigned int line, size_t size) if (!ret) my_syslog(LOG_ERR, _("failed to allocate %d bytes"), (int) size); - else if (option_bool(OPT_LOG_MALLOC)) + else if (daemon->log_malloc) my_syslog(LOG_INFO, _("malloc: %s:%u %zu bytes at %x"), func, line, size, hash_ptr(ret)); return ret; @@ -951,17 +951,20 @@ void *whine_realloc_real(const char *func, unsigned int line, void *ptr, size_t if (!ret) my_syslog(LOG_ERR, _("failed to reallocate %d bytes"), (int) size); - else if (option_bool(OPT_LOG_MALLOC)) + else if (daemon->log_malloc) my_syslog(LOG_INFO, _("realloc: %s:%u %zu bytes from %x to %x"), func, line, size, old, hash_ptr(ret)); return ret; } +#undef free void free_real(const char *func, unsigned int line, void *ptr) { - if (ptr && option_bool(OPT_LOG_MALLOC)) - my_syslog(LOG_INFO, _("free: %s:%u block at %x"), func, line, hash_ptr(ptr)); + if (ptr) + { + if (daemon->log_malloc) + my_syslog(LOG_INFO, _("free: %s:%u block at %x"), func, line, hash_ptr(ptr)); -#undef free - free(ptr); + free(ptr); + } }