Fix some potential iterator invalidation issues

This commit is contained in:
sfan5 2024-02-15 15:29:44 +01:00
parent 9ac6d330b4
commit 2b97fead9e
6 changed files with 12 additions and 17 deletions

@ -70,14 +70,13 @@ public:
}
void dereg(MtEvent::Type type, event_receive_func f, void *data) override
{
std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(type);
auto i = m_dest.find(type);
if (i != m_dest.end()) {
std::list<FuncSpec> &funcs = i->second.funcs;
auto j = funcs.begin();
while (j != funcs.end()) {
for (auto j = funcs.begin(); j != funcs.end(); ) {
bool remove = (j->f == f && (!data || j->d == data));
if (remove)
funcs.erase(j++);
j = funcs.erase(j);
else
++j;
}

@ -1336,15 +1336,14 @@ void Server::handleCommand_RemovedSounds(NetworkPacket* pkt)
*pkt >> id;
std::unordered_map<s32, ServerPlayingSound>::iterator i =
m_playing_sounds.find(id);
auto i = m_playing_sounds.find(id);
if (i == m_playing_sounds.end())
continue;
ServerPlayingSound &psound = i->second;
psound.clients.erase(pkt->getPeerId());
if (psound.clients.empty())
m_playing_sounds.erase(i++);
m_playing_sounds.erase(i);
}
}

@ -2924,12 +2924,11 @@ void Server::DeleteClient(session_t peer_id, ClientDeletionReason reason)
/*
Clear references to playing sounds
*/
for (std::unordered_map<s32, ServerPlayingSound>::iterator
i = m_playing_sounds.begin(); i != m_playing_sounds.end();) {
for (auto i = m_playing_sounds.begin(); i != m_playing_sounds.end();) {
ServerPlayingSound &psound = i->second;
psound.clients.erase(peer_id);
if (psound.clients.empty())
m_playing_sounds.erase(i++);
i = m_playing_sounds.erase(i);
else
++i;
}

@ -54,7 +54,6 @@ class IWritableItemDefManager;
class NodeDefManager;
class IWritableCraftDefManager;
class BanManager;
class EventManager;
class Inventory;
class ModChannelMgr;
class RemotePlayer;

@ -123,9 +123,9 @@ void BanManager::add(const std::string &ip, const std::string &name)
void BanManager::remove(const std::string &ip_or_name)
{
MutexAutoLock lock(m_mutex);
for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
for (auto it = m_ips.begin(); it != m_ips.end();) {
if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
m_ips.erase(it++);
it = m_ips.erase(it);
m_modified = true;
} else {
++it;

@ -1616,9 +1616,8 @@ void ServerEnvironment::step(float dtime)
Manage particle spawner expiration
*/
if (m_particle_management_interval.step(dtime, 1.0)) {
for (std::unordered_map<u32, float>::iterator i = m_particle_spawners.begin();
i != m_particle_spawners.end(); ) {
//non expiring spawners
for (auto i = m_particle_spawners.begin(); i != m_particle_spawners.end(); ) {
// non expiring spawners
if (i->second == PARTICLE_SPAWNER_NO_EXPIRY) {
++i;
continue;
@ -1626,7 +1625,7 @@ void ServerEnvironment::step(float dtime)
i->second -= 1.0f;
if (i->second <= 0.f)
m_particle_spawners.erase(i++);
i = m_particle_spawners.erase(i);
else
++i;
}