Guarantee ActiveObjectMgr::m_active_object is not modified while iterating (#13468)

Currently if a mod creates new active objects in on_deactivate the server could crash.
This commit is contained in:
lhofhansl 2023-04-28 11:17:48 -07:00 committed by GitHub
parent 7f6b09dce8
commit b35aa10579
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 10 deletions

View File

@ -27,17 +27,15 @@ namespace server
void ActiveObjectMgr::clear(const std::function<bool(ServerActiveObject *, u16)> &cb)
{
std::vector<u16> objects_to_remove;
for (auto &it : m_active_objects) {
if (cb(it.second, it.first)) {
// Id to be removed from m_active_objects
objects_to_remove.push_back(it.first);
}
}
// make a defensive copy in case the
// passed callback changes the set of active objects
auto cloned_map(m_active_objects);
// Remove references from m_active_objects
for (u16 i : objects_to_remove) {
m_active_objects.erase(i);
for (auto &it : cloned_map) {
if (cb(it.second, it.first)) {
// Remove reference from m_active_objects
m_active_objects.erase(it.first);
}
}
}