From fa588eff5d62b50951d71998d0c5dc8264cf8109 Mon Sep 17 00:00:00 2001 From: WinterSolstice8 <60417494+wintersolstice8@users.noreply.github.com> Date: Thu, 16 Jan 2025 10:03:52 -0700 Subject: [PATCH] [core] Add safety around m_errmsg in states --- src/map/ai/states/ability_state.cpp | 11 +++++++++-- src/map/ai/states/attack_state.cpp | 11 ++++++++++- src/map/ai/states/item_state.cpp | 20 +++++++++++++++++--- src/map/ai/states/magic_state.cpp | 20 +++++++++++++++++--- src/map/ai/states/mobskill_state.cpp | 11 +++++++++-- src/map/ai/states/petskill_state.cpp | 11 +++++++++-- src/map/ai/states/range_state.cpp | 20 +++++++++++++++++--- src/map/ai/states/weaponskill_state.cpp | 11 +++++++++-- 8 files changed, 97 insertions(+), 18 deletions(-) diff --git a/src/map/ai/states/ability_state.cpp b/src/map/ai/states/ability_state.cpp index c9011147e3c..b96efbd611a 100644 --- a/src/map/ai/states/ability_state.cpp +++ b/src/map/ai/states/ability_state.cpp @@ -45,9 +45,16 @@ CAbilityState::CAbilityState(CBattleEntity* PEntity, uint16 targid, uint16 abili } auto* PTarget = m_PEntity->IsValidTarget(m_targid, PAbility->getValidTarget(), m_errorMsg); - if (!PTarget || m_errorMsg) + if (!PTarget || this->HasErrorMsg()) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } SetTarget(PTarget->targid); m_PAbility = std::make_unique(*PAbility); diff --git a/src/map/ai/states/attack_state.cpp b/src/map/ai/states/attack_state.cpp index 8a1fb2d53bd..e970ec87dc0 100644 --- a/src/map/ai/states/attack_state.cpp +++ b/src/map/ai/states/attack_state.cpp @@ -35,11 +35,20 @@ CAttackState::CAttackState(CBattleEntity* PEntity, uint16 targid) PEntity->SetBattleTargetID(targid); PEntity->SetBattleStartTime(server_clock::now()); CAttackState::UpdateTarget(); + if (!GetTarget() || m_errorMsg) { PEntity->SetBattleTargetID(0); - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } + if (PEntity->PAI->PathFind) { PEntity->PAI->PathFind->Clear(); diff --git a/src/map/ai/states/item_state.cpp b/src/map/ai/states/item_state.cpp index b0d89eb770f..8cab2f5a457 100644 --- a/src/map/ai/states/item_state.cpp +++ b/src/map/ai/states/item_state.cpp @@ -80,9 +80,16 @@ CItemState::CItemState(CCharEntity* PEntity, uint16 targid, uint8 loc, uint8 slo UpdateTarget(PEntity->IsValidTarget(targid, m_PItem->getValidTarget(), m_errorMsg)); auto* PTarget = GetTarget(); - if (!PTarget || m_errorMsg) + if (!PTarget || this->HasErrorMsg()) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } auto [error, param, value] = luautils::OnItemCheck(PTarget, m_PItem, ITEMCHECK::NONE, m_PEntity); @@ -294,7 +301,14 @@ void CItemState::InterruptItem(action_t& action) actionTarget.messageID = 0; actionTarget.knockback = 0; - m_PEntity->pushPacket(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + m_PEntity->pushPacket(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } } diff --git a/src/map/ai/states/magic_state.cpp b/src/map/ai/states/magic_state.cpp index f8fac2b66c7..619da425578 100644 --- a/src/map/ai/states/magic_state.cpp +++ b/src/map/ai/states/magic_state.cpp @@ -50,14 +50,28 @@ CMagicState::CMagicState(CBattleEntity* PEntity, uint16 targid, SpellID spellid, m_PSpell = PSpell->clone(); auto* PTarget = m_PEntity->IsValidTarget(m_targid, m_PSpell->getValidTarget(), m_errorMsg); - if (!PTarget || m_errorMsg) + if (!PTarget || this->HasErrorMsg()) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } if (!CanCastSpell(PTarget, false)) { - throw CStateInitException(m_errorMsg->copy()); + if (HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } auto errorMsg = luautils::OnMagicCastingCheck(m_PEntity, PTarget, GetSpell()); diff --git a/src/map/ai/states/mobskill_state.cpp b/src/map/ai/states/mobskill_state.cpp index 1c28906ba8b..01285bab9a3 100644 --- a/src/map/ai/states/mobskill_state.cpp +++ b/src/map/ai/states/mobskill_state.cpp @@ -47,9 +47,16 @@ CMobSkillState::CMobSkillState(CBattleEntity* PEntity, uint16 targid, uint16 wsi auto* PTarget = m_PEntity->IsValidTarget(m_targid, skill->getValidTargets(), m_errorMsg); - if (!PTarget || m_errorMsg) + if (!PTarget | this->HasErrorMsg()) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } m_PSkill = std::make_unique(*skill); diff --git a/src/map/ai/states/petskill_state.cpp b/src/map/ai/states/petskill_state.cpp index f68a67d11dc..088b9ecef1a 100644 --- a/src/map/ai/states/petskill_state.cpp +++ b/src/map/ai/states/petskill_state.cpp @@ -46,9 +46,16 @@ CPetSkillState::CPetSkillState(CPetEntity* PEntity, uint16 targid, uint16 wsid) auto* PTarget = m_PEntity->IsValidTarget(m_targid, skill->getValidTargets(), m_errorMsg); - if (!PTarget || m_errorMsg) + if (!PTarget || this->HasErrorMsg()) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } m_PSkill = std::make_unique(*skill); diff --git a/src/map/ai/states/range_state.cpp b/src/map/ai/states/range_state.cpp index 20211e2391b..444306ff13d 100644 --- a/src/map/ai/states/range_state.cpp +++ b/src/map/ai/states/range_state.cpp @@ -36,14 +36,28 @@ CRangeState::CRangeState(CBattleEntity* PEntity, uint16 targid) { auto* PTarget = m_PEntity->IsValidTarget(m_targid, TARGET_ENEMY, m_errorMsg); - if (!PTarget || m_errorMsg) + if (!PTarget || this->HasErrorMsg()) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } if (!CanUseRangedAttack(PTarget, false)) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } if (distance(m_PEntity->loc.p, PTarget->loc.p) > 25) diff --git a/src/map/ai/states/weaponskill_state.cpp b/src/map/ai/states/weaponskill_state.cpp index e4ec93a313f..29f3e67c2aa 100644 --- a/src/map/ai/states/weaponskill_state.cpp +++ b/src/map/ai/states/weaponskill_state.cpp @@ -42,9 +42,16 @@ CWeaponSkillState::CWeaponSkillState(CBattleEntity* PEntity, uint16 targid, uint auto target_flags = battleutils::isValidSelfTargetWeaponskill(wsid) ? TARGET_SELF : TARGET_ENEMY; auto* PTarget = m_PEntity->IsValidTarget(m_targid, target_flags, m_errorMsg); - if (!PTarget || m_errorMsg) + if (!PTarget || this->HasErrorMsg()) { - throw CStateInitException(m_errorMsg->copy()); + if (this->HasErrorMsg()) + { + throw CStateInitException(m_errorMsg->copy()); + } + else + { + throw CStateInitException(std::make_unique()); + } } if (!m_PEntity->CanSeeTarget(PTarget, false))