From 59292b15f63d7a77f558f1c82fee376c542fec40 Mon Sep 17 00:00:00 2001 From: Chris Miles Date: Tue, 11 Feb 2025 21:22:56 -0600 Subject: [PATCH] [Loginserver] Fix iterator crash (#4670) --- loginserver/client_manager.cpp | 77 ++++++++++++++++------------------ 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/loginserver/client_manager.cpp b/loginserver/client_manager.cpp index b4f84c7c7..8f283c973 100644 --- a/loginserver/client_manager.cpp +++ b/loginserver/client_manager.cpp @@ -199,7 +199,8 @@ ClientManager::ClientManager() ); } -ClientManager::~ClientManager() { +ClientManager::~ClientManager() +{ delete m_titanium_stream; delete m_titanium_ops; delete m_sod_stream; @@ -210,53 +211,49 @@ void ClientManager::Process() { ProcessDisconnect(); - m_clients.erase( - std::remove_if( - m_clients.begin(), m_clients.end(), - [](Client *c) { - if (!c->Process()) { - LogWarning("Client had a fatal error and had to be removed from the login"); - delete c; - return true; - } - return false; - } - ), - m_clients.end()); + for (auto it = m_clients.begin(); it != m_clients.end();) { + Client *c = *it; + if (!c->Process()) { + LogWarning("Client had a fatal error and had to be removed from the login"); + delete c; + it = m_clients.erase(it); + } + else { + ++it; + } + } } void ClientManager::ProcessDisconnect() { - m_clients.erase( - std::remove_if( - m_clients.begin(), m_clients.end(), - [](Client *c) { - if (c->GetConnection()->CheckState(CLOSED)) { - LogInfo("Client disconnected from the server, removing client"); - delete c; - return true; - } - return false; - } - ), - m_clients.end()); + auto it = m_clients.begin(); + while (it != m_clients.end()) { + Client *c = *it; + if (c->GetConnection()->CheckState(CLOSED)) { + LogInfo("Client disconnected from the server, removing client"); + delete c; + it = m_clients.erase(it); + } + else { + ++it; + } + } } void ClientManager::RemoveExistingClient(unsigned int account_id, const std::string &loginserver) { - m_clients.erase( - std::remove_if( - m_clients.begin(), m_clients.end(), - [&](Client *c) { - if (c->GetAccountID() == account_id && c->GetLoginServerName() == loginserver) { - LogInfo("Client attempting to log in existing client already logged in, removing existing client"); - delete c; - return true; - } - return false; - } - ), - m_clients.end()); + auto it = m_clients.begin(); + while (it != m_clients.end()) { + Client *c = *it; + if (c->GetAccountID() == account_id && c->GetLoginServerName() == loginserver) { + LogInfo("Client attempting to log in existing client already logged in, removing existing client"); + delete c; + it = m_clients.erase(it); + } + else { + ++it; + } + } } Client *ClientManager::GetClient(unsigned int account_id, const std::string &loginserver)