Skip to content

Commit

Permalink
[10927] Prevent null-pointer dereference in AI calls
Browse files Browse the repository at this point in the history
  • Loading branch information
zergtmn committed Dec 28, 2010
1 parent 61d8898 commit b1f3d8e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/game/Creature.cpp
Expand Up @@ -456,7 +456,8 @@ void Creature::Update(uint32 update_diff, uint32 diff)
SetDeathState( JUST_ALIVED );

//Call AI respawn virtual function
i_AI->JustRespawned();
if (AI())
AI()->JustRespawned();

GetMap()->Add(this);
}
Expand Down Expand Up @@ -529,10 +530,13 @@ void Creature::Update(uint32 update_diff, uint32 diff)

if(!IsInEvadeMode())
{
// do not allow the AI to be changed during update
m_AI_locked = true;
i_AI->UpdateAI(diff); // AI not react good at real update delays (while freeze in non-active part of map)
m_AI_locked = false;
if (AI())
{
// do not allow the AI to be changed during update
m_AI_locked = true;
AI()->UpdateAI(diff); // AI not react good at real update delays (while freeze in non-active part of map)
m_AI_locked = false;
}
}

// creature can be dead after UpdateAI call
Expand Down
2 changes: 1 addition & 1 deletion src/shared/revision_nr.h
@@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
#define REVISION_NR "10926"
#define REVISION_NR "10927"
#endif // __REVISION_NR_H__

2 comments on commit b1f3d8e

@kamikazetg
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be preferable to do:

if(!IsInEvadeMode() && AI()) or if(AI() && !IsInEvadeMode())

Instead of two Ifs? Just a thought.

@VladimirMangos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe or maybe not. This is checks for different states. I meaning: selected by its cases can be for diff purpose.
For example if we in future get some else case for IsInEvadeMode() then AI() check will be not good placed.
In current state impossible say how better :) Maybe for more clear code better all internal if move to Creature::UpdateAI helper function.

Please sign in to comment.