From e0dc85a5f18e6f1c84d613352608bf4ff06ea59f Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Mon, 14 Nov 2011 18:30:16 +0000 Subject: [PATCH] fix a crash in the server if a slave server is unregistered --- src/serverlist.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/serverlist.cpp b/src/serverlist.cpp index 6ac80ae6..98471ad3 100755 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -90,7 +90,7 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum, } } - // per definition, the first entry in the server list it the own server + // per definition, the first entry in the server list is the own server ServerList.append ( ThisServerListEntry ); // parse the predefined server infos (if any) according to definition: @@ -234,8 +234,10 @@ void CServerListManager::OnTimerPollList() { QMutexLocker locker ( &Mutex ); - // check all list entries except of the very first one (which is the central - // server entry) and the predefined servers if they are still valid + // Check all list entries except of the very first one (which is the central + // server entry) and the predefined servers if they are still valid. + // Note that we have to use "ServerList.size()" function in the for loop + // since we may remove elements from the server list inside the for loop. for ( int iIdx = 1 + iNumPredefinedServers; iIdx < ServerList.size(); iIdx++ ) { // 1 minute = 60 * 1000 ms @@ -316,7 +318,7 @@ void CServerListManager::CentralServerUnregisterServer ( const CHostAddress& Ine // Find the server to unregister in the list. The very first list entry // must not be checked since this is per definition the central server - // (i.e., this server), also the predefined servers must not be checked + // (i.e., this server), also the predefined servers must not be checked. for ( int iIdx = 1 + iNumPredefinedServers; iIdx < iCurServerListSize; iIdx++ ) { if ( ServerList[iIdx].HostAddr == InetAddr ) @@ -324,8 +326,11 @@ void CServerListManager::CentralServerUnregisterServer ( const CHostAddress& Ine // remove this list entry ServerList.removeAt ( iIdx ); - // entry found, leave for-loop - continue; + // entry found, leave for-loop (it is important to exit the + // for loop since when we remove an item from the server list, + // "iCurServerListSize" is not correct anymore and we could get + // a segmentation fault) + break; } } }