From 3712db9cc6365e556a803daf32416ae929e95330 Mon Sep 17 00:00:00 2001 From: peavey Date: Sun, 17 Dec 2006 02:46:47 +0000 Subject: Invented safe delete-while-itering for hash_map. git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6029 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/command_parse.h | 2 ++ src/command_parse.cpp | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/command_parse.h b/include/command_parse.h index 98e189f9a..b9e57035d 100644 --- a/include/command_parse.h +++ b/include/command_parse.h @@ -175,6 +175,8 @@ class CommandParser : public classbase */ bool RemoveCommands(const char* source); + void RemoveCommand(nspace::hash_map::iterator safei, const char* source); + /** Add a new command to the commands hash * @param f The new command_t to add to the list * @param so_handle The handle to the shared object where the command can be found. diff --git a/src/command_parse.cpp b/src/command_parse.cpp index e56005751..3ce91afda 100644 --- a/src/command_parse.cpp +++ b/src/command_parse.cpp @@ -452,24 +452,34 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd) bool CommandParser::RemoveCommands(const char* source) { - nspace::hash_map::iterator i, safeiter, last_iter; - last_iter = cmdlist.begin(); - + nspace::hash_map::iterator i,safei; for (i = cmdlist.begin(); i != cmdlist.end(); i++) { - if (i->second->source == std::string(source)) + safei = i; + safei++; + if (safei != cmdlist.end()) { - ServerInstance->Log(DEBUG, "removecommands(%s) Removing dependent command: %s", i->second->source.c_str(), i->second->command.c_str()); - safeiter = i; - i = last_iter; - cmdlist.erase(safeiter); - continue; + RemoveCommand(safei, source); } - last_iter = i; + } + safei = cmdlist.begin(); + if (safei != cmdlist.end()) + { + RemoveCommand(safei, source); } return true; } +void CommandParser::RemoveCommand(nspace::hash_map::iterator safei, const char* source) +{ + command_t* x = safei->second; + if (x->source == std::string(source)) + { + ServerInstance->Log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str()); + cmdlist.erase(safei); + } +} + void CommandParser::ProcessBuffer(std::string &buffer,userrec *user) { std::string::size_type a; -- cgit v1.2.3