Skip to content

Commit 6dcae60

Browse files
committed
More null pointer deref fixes
1 parent ce51079 commit 6dcae60

1 file changed

Lines changed: 34 additions & 32 deletions

File tree

utils/RCBot2_meta/bot_hldm_bot.cpp

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -226,37 +226,40 @@ bool CHLDMBot :: executeAction (const eBotAction iAction)
226226
models/items/ammocrate_smg1.mdl
227227
*/
228228

229-
const char* szModel = m_pAmmoCrate.get()->GetIServerEntity()->GetModelName().ToCStr();
230-
const char type = szModel[23];
231-
232-
if ( type == 'a' ) // ar2
233-
{
234-
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_AR2));
235-
}
236-
else if ( type == 'g' ) // grenade
237-
{
238-
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_FRAG));
239-
}
240-
else if ( type == 'r' ) // rocket
241-
{
242-
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_RPG));
243-
}
244-
else if ( type == 's' ) // smg
229+
// Ensure m_pAmmoCrate is not null before dereferencing - [APG]RoboCop[CL]
230+
if (m_pAmmoCrate)
245231
{
246-
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_SMG1));
247-
}
232+
const char* szModel = m_pAmmoCrate.get()->GetIServerEntity()->GetModelName().ToCStr();
233+
const char type = szModel[23];
248234

249-
if ( pWeapon && pWeapon->getAmmo(this) < 1 )
250-
{
251-
CBotSchedule *pSched = new CBotSchedule();
252-
253-
pSched->addTask(new CFindPathTask(m_pAmmoCrate));
254-
pSched->addTask(new CBotHL2DMUseButton(m_pAmmoCrate));
235+
if (type == 'a') // ar2
236+
{
237+
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_AR2));
238+
}
239+
else if (type == 'g') // grenade
240+
{
241+
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_FRAG));
242+
}
243+
else if (type == 'r') // rocket
244+
{
245+
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_RPG));
246+
}
247+
else if (type == 's') // smg
248+
{
249+
pWeapon = m_pWeapons->getWeapon(CWeapons::getWeapon(HL2DM_WEAPON_SMG1));
250+
}
251+
if (pWeapon && pWeapon->getAmmo(this) < 1)
252+
{
253+
CBotSchedule* pSched = new CBotSchedule();
255254

256-
m_pSchedules->add(pSched);
255+
pSched->addTask(new CFindPathTask(m_pAmmoCrate));
256+
pSched->addTask(new CBotHL2DMUseButton(m_pAmmoCrate));
257257

258-
m_fUtilTimes[iAction] = engine->Time() + randomFloat(5.0f,10.0f);
259-
return true;
258+
m_pSchedules->add(pSched);
259+
260+
m_fUtilTimes[iAction] = engine->Time() + randomFloat(5.0f, 10.0f);
261+
return true;
262+
}
260263
}
261264
}
262265
return false;
@@ -518,18 +521,17 @@ void CHLDMBot :: getTasks (unsigned iIgnore)
518521
}
519522
}
520523

521-
if ( m_pNearbyWeapon.get() )
524+
if (const edict_t *nearbyWeapon = m_pNearbyWeapon.get())
522525
{
523526
static CWeapon *pWeapon;
524-
pWeapon = CWeapons::getWeapon(m_pNearbyWeapon.get()->GetClassName());
527+
pWeapon = CWeapons::getWeapon(nearbyWeapon->GetClassName());
525528

526-
if ( pWeapon && !m_pWeapons->hasWeapon(pWeapon->getID()) )
529+
if (pWeapon && !m_pWeapons->hasWeapon(pWeapon->getID()))
527530
{
528-
ADD_UTILITY(BOT_UTIL_PICKUP_WEAPON, true , 0.6f + pWeapon->getPreference()*0.1f)
531+
ADD_UTILITY(BOT_UTIL_PICKUP_WEAPON, true, 0.6f + static_cast<float>(pWeapon->getPreference()) * 0.1f)
529532
}
530533
}
531534

532-
533535
utils.execute();
534536

535537
while ( (next = utils.nextBest()) != nullptr)

0 commit comments

Comments
 (0)