Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameengine): two dereferences that only a shipping build reaches

Olcay Seygan20a95f0400ff
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
index d8477f5b7..21cba0990 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/TunnelTracker.cpp
@@ -217,7 +217,13 @@ void TunnelTracker::onTunnelCreated( const Object *newTunnel )
// ------------------------------------------------------------------------
void TunnelTracker::onTunnelDestroyed( const Object *deadTunnel )
{
- m_tunnelCount--;
+ //
+ // m_tunnelCount is unsigned, so a decrement at zero does not go negative, it goes to four
+ // billion - and the branch below then takes the "there is still a tunnel standing" path with
+ // nothing standing.
+ //
+ if( m_tunnelCount > 0 )
+ m_tunnelCount--;
m_tunnelIDs.remove( deadTunnel->getID() );
if( m_tunnelCount == 0 )
@@ -229,7 +235,11 @@ void TunnelTracker::onTunnelDestroyed( const Object *deadTunnel )
}
else
{
- Object *validTunnel = TheGameLogic->findObjectByID( m_tunnelIDs.front() );
+ // front() on an empty list reads whatever the list's head happens to point at. The loop
+ // below already copes with a null tunnel, so ask for one only when there is one.
+ Object *validTunnel = m_tunnelIDs.empty()
+ ? NULL
+ : TheGameLogic->findObjectByID( m_tunnelIDs.front() );
// Otherwise, make sure nobody inside remembers the dead tunnel as the one they entered
// (scripts need to use so there must be something valid here)
for(ContainedItemsList::iterator it = m_containList.begin(); it != m_containList.end(); )
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp
index b3a6ea957..d6fc455aa 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp
@@ -246,8 +246,10 @@ void ThingFactory::reset( void )
// they must depend only on the data loaded, not on how many matches this process has played.
// m_nextTemplateID kept counting up across resets; it now goes back to just past the templates
// that survived.
+ // The assert is compiled out of a shipping build, and the line under it used to dereference
+ // whatever the assert had just complained about.
DEBUG_ASSERTCRASH( m_firstTemplate != NULL, ("no templates left after deleting overrides") );
- m_nextTemplateID = (UnsignedShort)(m_firstTemplate->getTemplateID() + 1);
+ m_nextTemplateID = m_firstTemplate ? (UnsignedShort)(m_firstTemplate->getTemplateID() + 1) : 1;
} // end reset
//-------------------------------------------------------------------------------------------------