From 3faa3c639afcda1fbd7b3d000a12866c047c7007 Mon Sep 17 00:00:00 2001 From: EricksOliveira Date: Sat, 29 Mar 2025 06:58:57 -0300 Subject: [PATCH] This update adds null pointer checks and extra validations to the AttackAnythingAction::isUseful() method, preventing invalid accesses that could cause crashes. (#1135) Checking bots and botAI before method calls. Validating targets and confirming if they are still in the world (IsInWorld()). Adding debug logs (LOG_DEBUG) to make it easier to identify issues. --- src/strategy/actions/ChooseTargetActions.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/strategy/actions/ChooseTargetActions.cpp b/src/strategy/actions/ChooseTargetActions.cpp index e4df1e02..c5713369 100644 --- a/src/strategy/actions/ChooseTargetActions.cpp +++ b/src/strategy/actions/ChooseTargetActions.cpp @@ -31,7 +31,10 @@ bool AttackEnemyFlagCarrierAction::isUseful() bool AttackAnythingAction::isUseful() { - if (!botAI->AllowActivity(GRIND_ACTIVITY)) // Bot not allowed to be active + if (!bot || !botAI) // Prevents invalid accesses + return false; + + if (!botAI->AllowActivity(GRIND_ACTIVITY)) // Bot cannot be active return false; if (botAI->HasStrategy("stay", BOT_STATE_NON_COMBAT)) @@ -41,19 +44,17 @@ bool AttackAnythingAction::isUseful() return false; Unit* target = GetTarget(); - - if (!target) + if (!target || !target->IsInWorld()) // Checks if the target is valid and in the world return false; std::string const name = std::string(target->GetName()); - // Check for invalid targets: Dummy, Charge Target, Melee Target, Ranged Target if (!name.empty() && (name.find("Dummy") != std::string::npos || name.find("Charge Target") != std::string::npos || name.find("Melee Target") != std::string::npos || name.find("Ranged Target") != std::string::npos)) { - return false; // Target is one of the disallowed types + return false; } return true;