Skip to content

Commit

Permalink
fix(Save/Restore): Fix dereferencing null if m_iActiveSound is no lon…
Browse files Browse the repository at this point in the history
…ger valid index by save restore.

See ValveSoftware#381
  • Loading branch information
FriskTheFallenHuman committed Nov 30, 2022
1 parent d577a40 commit 2769eed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
10 changes: 8 additions & 2 deletions mp/src/game/server/ai_basenpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3634,7 +3634,10 @@ void CAI_BaseNPC::UpdateEfficiency( bool bInPVS )
}
}

iSound = pCurrentSound->NextSound();
if ( pCurrentSound )
iSound = pCurrentSound->NextSound();
else
break;
}
}

Expand Down Expand Up @@ -3824,7 +3827,10 @@ void CAI_BaseNPC::UpdateSleepState( bool bInPVS )
break;
}

iSound = pCurrentSound->NextSound();
if ( pCurrentSound )
iSound = pCurrentSound->NextSound();
else
break;
}
}
}
Expand Down
59 changes: 31 additions & 28 deletions mp/src/game/server/ai_senses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@ void CAI_Senses::Listen( void )

if ( pCurrentSound && (iSoundMask & pCurrentSound->SoundType()) && CanHearSound( pCurrentSound ) )
{
// the npc cares about this sound, and it's close enough to hear.
// the npc cares about this sound, and it's close enough to hear.
pCurrentSound->m_iNextAudible = m_iAudibleList;
m_iAudibleList = iSound;
}

iSound = pCurrentSound->NextSound();
if ( pCurrentSound )
iSound = pCurrentSound->NextSound();
else
break;
}
}

Expand Down Expand Up @@ -423,16 +426,16 @@ int CAI_Senses::LookForHighPriorityEntities( int iDistance )
}

EndGather( nSeen, &m_SeenHighPriority );
}
else
{
for ( int i = m_SeenHighPriority.Count() - 1; i >= 0; --i )
{
if ( m_SeenHighPriority[i].Get() == NULL )
m_SeenHighPriority.FastRemove( i );
}
nSeen = m_SeenHighPriority.Count();
}
}
else
{
for ( int i = m_SeenHighPriority.Count() - 1; i >= 0; --i )
{
if ( m_SeenHighPriority[i].Get() == NULL )
m_SeenHighPriority.FastRemove( i );
}
nSeen = m_SeenHighPriority.Count();
}

return nSeen;
}
Expand Down Expand Up @@ -480,24 +483,24 @@ int CAI_Senses::LookForNPCs( int iDistance )
// Fall through
}

for ( int i = m_SeenNPCs.Count() - 1; i >= 0; --i )
{
if ( m_SeenNPCs[i].Get() == NULL )
for ( int i = m_SeenNPCs.Count() - 1; i >= 0; --i )
{
if ( m_SeenNPCs[i].Get() == NULL )
{
m_SeenNPCs.FastRemove( i );
m_SeenNPCs.FastRemove( i );
}
else if ( bRemoveStaleFromCache )
{
if ( ( !((CAI_BaseNPC *)m_SeenNPCs[i].Get())->ShouldNotDistanceCull() &&
origin.DistToSqr(m_SeenNPCs[i]->GetAbsOrigin()) > distSq ) ||
!Look( m_SeenNPCs[i] ) )
{
m_SeenNPCs.FastRemove( i );
m_SeenNPCs.FastRemove( i );
}
}
}
}

return m_SeenNPCs.Count();
return m_SeenNPCs.Count();
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -532,15 +535,15 @@ int CAI_Senses::LookForObjects( int iDistance )

EndGather( nSeen, &m_SeenMisc );
}
else
{
for ( int i = m_SeenMisc.Count() - 1; i >= 0; --i )
{
if ( m_SeenMisc[i].Get() == NULL )
m_SeenMisc.FastRemove( i );
}
nSeen = m_SeenMisc.Count();
}
else
{
for ( int i = m_SeenMisc.Count() - 1; i >= 0; --i )
{
if ( m_SeenMisc[i].Get() == NULL )
m_SeenMisc.FastRemove( i );
}
nSeen = m_SeenMisc.Count();
}

return nSeen;
}
Expand Down
6 changes: 4 additions & 2 deletions mp/src/game/server/envmicrophone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,11 @@ void CEnvMicrophone::Think(void)
fHearSound = true;
}
}
}

nSound = pCurrentSound->NextSound();
nSound = pCurrentSound->NextSound();
}
else
break;
}

if( fHearSound )
Expand Down

0 comments on commit 2769eed

Please sign in to comment.