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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user