Fix race condition in log_println (use-after-free)

The check for logready was done BEFORE acquiring the lock, causing a race
condition with log_close(). If log_close() runs between the check and
the lock acquisition, the log file would be closed but log_println would
still try to write to it (use-after-free).

Now the logready/logp check is inside the critical section.
This commit is contained in:
2025-12-25 23:04:13 +03:00
parent afa4a3a4ea
commit 0353d31355

View File

@@ -70,11 +70,14 @@ log_init(stbbr_log_t loglevel)
void
log_println(stbbr_log_t loglevel, const char * const msg, ...)
{
if (!logready || loglevel < minlevel) {
pthread_mutex_lock(&loglock);
// Check logready AFTER acquiring lock to prevent race with log_close
if (!logready || !logp || loglevel < minlevel) {
pthread_mutex_unlock(&loglock);
return;
}
pthread_mutex_lock(&loglock);
va_list arg;
va_start(arg, msg);
GString *fmt_msg = g_string_new(NULL);