From da33c5c30fe2b76a3fbd3751027090691031230c Mon Sep 17 00:00:00 2001 From: gsemaj Date: Sun, 13 Aug 2023 18:26:30 -0700 Subject: [PATCH] Make `clearBuffs` memory-safe improperly erasing during iteration oops --- src/Combat.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Combat.cpp b/src/Combat.cpp index 7a46cfa..aa8b912 100644 --- a/src/Combat.cpp +++ b/src/Combat.cpp @@ -61,14 +61,13 @@ void Player::removeBuff(int buffId, BuffClass buffClass) { } void Player::clearBuffs(bool force) { - for(auto buff : buffs) { - if(!force) { - removeBuff(buff.first); - } else { - delete buff.second; - } + auto it = buffs.begin(); + while(it != buffs.end()) { + Buff* buff = (*it).second; + if(!force) buff->clear(); + delete buff; + it = buffs.erase(it); } - buffs.clear(); } bool Player::hasBuff(int buffId) { @@ -217,14 +216,13 @@ void CombatNPC::removeBuff(int buffId, BuffClass buffClass) { } void CombatNPC::clearBuffs(bool force) { - for(auto buff : buffs) { - if(!force) { - removeBuff(buff.first); - } else { - delete buff.second; - } + auto it = buffs.begin(); + while(it != buffs.end()) { + Buff* buff = (*it).second; + if(!force) buff->clear(); + delete buff; + it = buffs.erase(it); } - buffs.clear(); } bool CombatNPC::hasBuff(int buffId) {