memdebug: make logging thread-safe

This should make it work fine even when using threads, like in the
threaded resolver.
This commit is contained in:
Daniel Stenberg 2025-11-27 12:25:34 +01:00
parent aa9342058f
commit c7ca9ba1ac
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
5 changed files with 25 additions and 27 deletions

View file

@ -30,6 +30,7 @@
#include "urldata.h"
#include "curlx/fopen.h" /* for CURLX_FOPEN_LOW(), CURLX_FREOPEN_LOW() */
#include "easy_lock.h"
#ifdef USE_BACKTRACE
#include "backtrace.h"
@ -66,6 +67,11 @@ static long memsize = 0; /* set number of mallocs allowed */
static struct backtrace_state *btstate;
#endif
/* lock access to the debug log */
static Curl_simple_lock debug_lock = CURL_SIMPLE_LOCK_INIT;
#define memdebug_lock() Curl_simple_lock_lock(&debug_lock)
#define memdebug_unlock() Curl_simple_lock_unlock(&debug_lock)
/* LeakSantizier (LSAN) calls _exit() instead of exit() when a leak is detected
on exit so the logfile must be closed explicitly or data could be lost.
Though _exit() does not call atexit handlers such as this, LSAN's call to
@ -525,8 +531,11 @@ void curl_dbg_log(const char *format, ...)
if(nchars > (int)sizeof(buf) - 1)
nchars = (int)sizeof(buf) - 1;
if(nchars > 0)
if(nchars > 0) {
memdebug_lock();
(fwrite)(buf, 1, (size_t)nchars, curl_dbg_logfile);
memdebug_unlock();
}
}
#endif /* CURLDEBUG */