mirror of
https://github.com/minetest/minetest.git
synced 2024-11-27 01:53:45 +01:00
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:
parent
7f6b09dce8
commit
b35aa10579
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user