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

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