From 28a4db0355709bcb14554c26c6f6d74e94d80e25 Mon Sep 17 00:00:00 2001 From: Asdow <20314541+Asdow@users.noreply.github.com> Date: Sat, 10 Feb 2024 17:18:19 +0200 Subject: [PATCH] Guard against nullptr dereference (#278) Encountered a situation where a pSoldier between bFirstID and bLastID was null and then we'd dereference it. --- Tactical/Auto Bandage.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Tactical/Auto Bandage.cpp b/Tactical/Auto Bandage.cpp index 0205376bf..a70f5f0ae 100644 --- a/Tactical/Auto Bandage.cpp +++ b/Tactical/Auto Bandage.cpp @@ -460,7 +460,7 @@ void AutoBandage( BOOLEAN fStart ) for ( pSoldier = MercPtrs[cnt]; cnt <= gTacticalStatus.Team[OUR_TEAM].bLastID; ++cnt, ++pSoldier ) { // 0verhaul: Make sure the merc is also in the sector before making him stand up! - if ( pSoldier->bActive && pSoldier->bInSector ) + if (pSoldier && pSoldier->bActive && pSoldier->bInSector) { ActionDone( pSoldier ); if ( pSoldier->bSlotItemTakenFrom != NO_SLOT ) @@ -483,12 +483,16 @@ void AutoBandage( BOOLEAN fStart ) ubLoop = gTacticalStatus.Team[ gbPlayerNum ].bFirstID; for ( ; ubLoop <= gTacticalStatus.Team[ gbPlayerNum ].bLastID; ++ubLoop) { - ActionDone( MercPtrs[ ubLoop ] ); - - // If anyone is still doing aid animation, stop! - if ( MercPtrs[ ubLoop ]->usAnimState == GIVING_AID || MercPtrs[ ubLoop ]->usAnimState == GIVING_AID_PRN ) + pSoldier = MercPtrs[ubLoop]; + if (pSoldier && pSoldier->bActive && pSoldier->bInSector) { - MercPtrs[ ubLoop ]->SoldierGotoStationaryStance( ); + ActionDone(pSoldier); + + // If anyone is still doing aid animation, stop! + if (pSoldier->usAnimState == GIVING_AID || pSoldier->usAnimState == GIVING_AID_PRN) + { + pSoldier->SoldierGotoStationaryStance(); + } } }