Support This Project

Changeset 1997

Show
Ignore:
Timestamp:
06/24/08 05:51:54 (7 months ago)
Author:
virdiq
Message:

Protect static log buffer with a mutex to avoid a race condition that leads to attempted freeing of invalid memory allocations. Closes ticket #139.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libwzd-core/wzd_log.c

    r1994 r1997  
    6161#include "wzd_misc.h" 
    6262#include "wzd_user.h" 
     63#include "wzd_mutex.h" 
    6364 
    6465#include "wzd_debug.h" 
     
    6768 
    6869static struct memory_log_t _static_log; 
     70 
     71static wzd_mutex_t * _static_log_mutex = NULL; 
     72static int _static_log_enabled = 0; /* 0 = disabled */ 
    6973 
    7074static void _buffer_push(const char *); 
     
    8185  int i; 
    8286 
     87  _static_log_mutex = wzd_mutex_create(0); /* argument is unused */ 
     88  if (_static_log_mutex) { 
     89    _static_log.size = 100; /* last 100 messages */ 
     90    _static_log.data = malloc(_static_log.size * sizeof(char *)); 
     91    if (_static_log.data) { 
     92      memset(_static_log.data, 0, _static_log.size * sizeof(char *)); 
     93      _static_log_enabled = 1; 
     94    } 
     95  } 
     96 
    8397  for (i=0; i<MAX_LOG_CHANNELS; i++) { 
    8498    _log_channels[i].fd = -1; 
    8599    _log_channels[i].syslog = 0; 
    86100  } 
    87  
    88   _static_log.size = 100; /* last 100 messages */ 
    89   _static_log.data = malloc(_static_log.size * sizeof(char*)); 
    90   memset(_static_log.data,0,_static_log.size * sizeof(char*)); 
    91101 
    92102  return 0; 
     
    118128{ 
    119129  int i,j,fd; 
     130 
     131  _static_log_enabled = 0; 
     132  wzd_mutex_lock(_static_log_mutex); 
    120133 
    121134  for (i=0; i<MAX_LOG_CHANNELS; i++) 
     
    131144    } 
    132145 
    133   for (i=0; i<_static_log.size; i++) { 
    134     free(_static_log.data[i]); 
    135   } 
    136   free(_static_log.data); 
    137   _static_log.size = 0; 
     146  if (_static_log_enabled) { 
     147    for (i=0; i<_static_log.size; i++) { 
     148      if (_static_log.data[i]) 
     149        free(_static_log.data[i]); 
     150    } 
     151    free(_static_log.data); 
     152    _static_log.size = 0; 
     153  } 
     154 
     155  wzd_mutex_destroy(_static_log_mutex); 
     156  _static_log_mutex = NULL; 
    138157} 
    139158 
     
    234253      write(_log_channels[level].fd, buffer, strlen(buffer)); 
    235254    } 
    236  
    237     _buffer_push(buffer); 
     255     
     256    if (_static_log_enabled) { 
     257      if (!wzd_mutex_lock(_static_log_mutex)) { 
     258        _buffer_push(buffer); 
     259        wzd_mutex_unlock(_static_log_mutex); 
     260      } 
     261    } 
    238262 
    239263#ifndef _WIN32 
     
    504528struct memory_log_t * get_log_buffer(void) 
    505529{ 
    506   return (&_static_log); 
    507 
    508  
     530  if (_static_log_enabled) 
     531    return (&_static_log); 
     532  return NULL; 
     533
     534 
  • trunk/libwzd-core/wzd_site.c

    r1994 r1997  
    15611561{ 
    15621562  int i; 
    1563   struct memory_log_t * log = get_log_buffer()
    1564   wzd_string_t * buffer = str_allocate()
     1563  struct memory_log_t * log
     1564  wzd_string_t * buffer
    15651565  int lines_to_show = 10; 
    15661566  int offset = 0; 
    15671567  char *ptr; 
    15681568  unsigned long ul; 
     1569 
     1570  log = get_log_buffer(); 
     1571  if (!log) { 
     1572    send_message_with_args(501, context, "Static log buffer not enabled"); 
     1573    return -1; 
     1574  } 
     1575  buffer = str_allocate(); 
    15691576 
    15701577  /* check if we have an argument (the number of lines to display) */