From 01341cfb0a0ce48e2817c2c9b654e6b65fcae2df Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 29 Dec 2011 13:50:31 +0100 Subject: [PATCH] Fix some invalid iterator uses This fixes #96. When the last user in a partyline channel is deleted, the channel is deleted, too. This invalidates the iterator used in OnDeleteUser(). This fix is to increase the iterator before the channel can be deleted. After the above fix, znc still crashed due to another broken use of iterators. When a network is deleted, it takes all its clients with it (why aren't they just moved into the "no network"-state?"). However, deleting a CClient removes it from the network's list of clients via CClient::Disconnect(). This resulted in another invalid use of iterators. Signed-off-by: Uli Schlachter --- modules/partyline.cpp | 8 ++++++-- src/IRCNetwork.cpp | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/partyline.cpp b/modules/partyline.cpp index c77fb94b..25c2d869 100644 --- a/modules/partyline.cpp +++ b/modules/partyline.cpp @@ -123,8 +123,12 @@ public: virtual EModRet OnDeleteUser(CUser& User) { // Loop through each chan - for (set::iterator it = m_ssChannels.begin(); it != m_ssChannels.end(); ++it) { - RemoveUser(&User, *it, "KICK", "User deleted", true); + for (set::iterator it = m_ssChannels.begin(); it != m_ssChannels.end();) { + CPartylineChannel *pChan = *it; + // RemoveUser() might delete channels, so make sure our + // iterator doesn't break. + it++; + RemoveUser(&User, pChan, "KICK", "User deleted", true); } return CONTINUE; diff --git a/src/IRCNetwork.cpp b/src/IRCNetwork.cpp index 8d8c1c88..6676248a 100644 --- a/src/IRCNetwork.cpp +++ b/src/IRCNetwork.cpp @@ -181,8 +181,8 @@ CIRCNetwork::~CIRCNetwork() { } // Delete clients - for (vector::const_iterator it = m_vClients.begin(); it != m_vClients.end(); ++it) { - CZNC::Get().GetManager().DelSockByAddr(*it); + while (!m_vClients.empty()) { + CZNC::Get().GetManager().DelSockByAddr(m_vClients[0]); } m_vClients.clear();