From f4db39f490b54753ecf4acae9686ad61d30ea765 Mon Sep 17 00:00:00 2001 From: ehouse Date: Fri, 3 Jul 2009 14:54:21 +0000 Subject: [PATCH] synchronize reads of std::map to fix crash due to memory corruption. Apparently containers aren't thread-safe even for reads. --- xwords4/relay/configs.cpp | 20 ++++++++++++-------- xwords4/relay/configs.h | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/xwords4/relay/configs.cpp b/xwords4/relay/configs.cpp index 04992e9e7..9f3b155fd 100644 --- a/xwords4/relay/configs.cpp +++ b/xwords4/relay/configs.cpp @@ -25,6 +25,7 @@ #include #include "configs.h" +#include "mlock.h" #define MAX_LINE 128 @@ -46,6 +47,8 @@ RelayConfigs::InitConfigs( const char* cfile ) RelayConfigs::RelayConfigs( const char* cfile ) { + pthread_mutex_init( &m_values_mutex, NULL ); + /* There's an order here. Open multiple files, if present. File in /etc is first, but overridden by local file which is in turn overridden by file passed in. */ @@ -57,10 +60,10 @@ RelayConfigs::RelayConfigs( const char* cfile ) bool RelayConfigs::GetValueFor( const char* key, int* value ) { - map::const_iterator iter = m_values.find(key); - bool found = iter != m_values.end(); + char buf[32]; + bool found = GetValueFor( key, buf, sizeof(buf) ); if ( found ) { - *value = atoi( iter->second.c_str() ); + *value = atoi( buf ); } return found; } @@ -79,6 +82,7 @@ RelayConfigs::GetValueFor( const char* key, time_t* value ) bool RelayConfigs::GetValueFor( const char* key, char* buf, int len ) { + MutexLock ml( &m_values_mutex ); map::const_iterator iter = m_values.find(key); bool found = iter != m_values.end(); if ( found ) { @@ -90,9 +94,8 @@ RelayConfigs::GetValueFor( const char* key, char* buf, int len ) bool RelayConfigs::GetValueFor( const char* key, vector& ints ) { - map::const_iterator iter = m_values.find(key); - bool found = iter != m_values.end(); - const char* str = iter->second.c_str(); + char str[256]; + bool found = GetValueFor( key, str, sizeof(str) ); int len = strlen(str); char buf[len+1]; strcpy( buf, str ); @@ -104,7 +107,7 @@ RelayConfigs::GetValueFor( const char* key, vector& ints ) *end = '\0'; } - logf( XW_LOGINFO, "adding %s to ports", port ); + fprintf( stderr, "adding %s to ports\n", port ); ints.push_back( atoi(port) ); if ( !end ) { @@ -120,6 +123,7 @@ RelayConfigs::GetValueFor( const char* key, vector& ints ) void RelayConfigs::SetValueFor( const char* key, const char* value ) { + MutexLock ml( &m_values_mutex ); /* Does this leak in the case where we're replacing a value? */ m_values.insert( pair(string(key),string(value) ) ); } @@ -135,7 +139,7 @@ RelayConfigs::parse( const char* fname, ino_t prev ) if ( inode != prev ) { FILE* f = fopen( fname, "r" ); if ( f != NULL ) { - logf( XW_LOGINFO, "config: reading from %s", fname ); + fprintf( stderr, "config: reading from %s\n", fname ); char line[MAX_LINE]; for ( ; ; ) { diff --git a/xwords4/relay/configs.h b/xwords4/relay/configs.h index 48fb48226..27ffea5d1 100644 --- a/xwords4/relay/configs.h +++ b/xwords4/relay/configs.h @@ -55,6 +55,7 @@ class RelayConfigs { time_t m_allConnInterval; map m_values; + pthread_mutex_t m_values_mutex; static RelayConfigs* instance; };