Skip to content

Commit 808017e

Browse files
authored
[GEN][ZH] Fix undefined behavior in ThingTemplate::isEquivalentTo (#932)
1 parent 8109614 commit 808017e

File tree

14 files changed

+25
-23
lines changed

14 files changed

+25
-23
lines changed

Generals/Code/GameEngine/Include/GameLogic/PartitionManager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,9 @@ class PartitionFilterThing : public PartitionFilter
10901090
Bool m_match;
10911091

10921092
public:
1093-
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {}
1093+
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {
1094+
DEBUG_ASSERTCRASH(m_tThing != NULL, ("ThingTemplate for PartitionFilterThing is NULL"));
1095+
}
10941096
protected:
10951097
virtual Bool allow( Object *other );
10961098
#if defined(RTS_DEBUG)

Generals/Code/GameEngine/Source/Common/RTS/ScoreKeeper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Int ScoreKeeper::getTotalObjectsBuilt( const ThingTemplate *pTemplate )
174174
for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
175175
{
176176
const ThingTemplate *theTemplate = it->first;
177-
if (theTemplate->isEquivalentTo(pTemplate))
177+
if (theTemplate && theTemplate->isEquivalentTo(pTemplate))
178178
++count;
179179
}
180180
return count;

Generals/Code/GameEngine/Source/Common/System/BuildAssistant.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,9 +1218,8 @@ Bool BuildAssistant::isPossibleToMakeUnit( Object *builder, const ThingTemplate
12181218
// get this button
12191219
commandButton = commandSet->getCommandButton(i);
12201220
if( commandButton &&
1221-
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD ||
1222-
commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
1223-
commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
1221+
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD || commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
1222+
commandButton->getThingTemplate() && commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
12241223
foundCommand = commandButton;
12251224

12261225
} // end for i

Generals/Code/GameEngine/Source/Common/Thing/ThingTemplate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ const AudioEventRTS *ThingTemplate::getPerUnitSound(const AsciiString& soundName
13191319
Bool ThingTemplate::isEquivalentTo(const ThingTemplate* tt) const
13201320
{
13211321
// sanity
1322-
if (!(this && tt))
1322+
if (!tt)
13231323
return false;
13241324

13251325
// sanity

Generals/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void AISkirmishPlayer::processBaseBuilding( void )
232232
}
233233
}
234234
}
235-
if (powerPlan && powerInfo && !powerPlan->isEquivalentTo(bldgPlan)) {
235+
if (powerInfo && powerPlan && !powerPlan->isEquivalentTo(bldgPlan)) {
236236
if (!powerUnderConstruction) {
237237
bldgPlan = powerPlan;
238238
bldgInfo = powerInfo;

Generals/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
17381738
if (beacon)
17391739
{
17401740
const ThingTemplate *thing = TheThingFactory->findTemplate( beacon->getControllingPlayer()->getPlayerTemplate()->getBeaconTemplate() );
1741-
if (thing->isEquivalentTo(beacon->getTemplate()))
1741+
if (thing && thing->isEquivalentTo(beacon->getTemplate()))
17421742
{
17431743
if (beacon->getControllingPlayer() == thisPlayer)
17441744
{

GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,9 @@ class PartitionFilterThing : public PartitionFilter
11271127
Bool m_match;
11281128

11291129
public:
1130-
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {}
1130+
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {
1131+
DEBUG_ASSERTCRASH(m_tThing != NULL, ("ThingTemplate for PartitionFilterThing is NULL"));
1132+
}
11311133
protected:
11321134
virtual Bool allow( Object *other );
11331135
#if defined(RTS_DEBUG)

GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ static void doFindMostReadyWeaponForThing( Object *obj, void *userData )
13771377
return;
13781378
}
13791379

1380-
if( info->thing->isEquivalentTo( obj->getTemplate() ) )
1380+
if( info->thing && info->thing->isEquivalentTo( obj->getTemplate() ) )
13811381
{
13821382
if( !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION )
13831383
&& !obj->testStatus( OBJECT_STATUS_SOLD )
@@ -1409,7 +1409,7 @@ static void doFindMostReadySpecialPowerForThing( Object *obj, void *userData )
14091409
return;
14101410
}
14111411

1412-
if( info->thing->isEquivalentTo( obj->getTemplate() ) )
1412+
if( info->thing && info->thing->isEquivalentTo( obj->getTemplate() ) )
14131413
{
14141414
if( !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION )
14151415
&& !obj->testStatus( OBJECT_STATUS_SOLD )
@@ -1445,7 +1445,7 @@ static void doFindExistingObjectWithThingTemplate( Object *obj, void *userData )
14451445
return;
14461446
}
14471447

1448-
if( info->thing->isEquivalentTo( obj->getTemplate() ) )
1448+
if( info->thing && info->thing->isEquivalentTo( obj->getTemplate() ) )
14491449
{
14501450
if( !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION )
14511451
&& !obj->testStatus( OBJECT_STATUS_SOLD )
@@ -2863,7 +2863,7 @@ static void countExisting( Object *obj, void *userData )
28632863
TypeCountData *typeCountData = (TypeCountData *)userData;
28642864

28652865
// Compare templates
2866-
if ( typeCountData->type->isEquivalentTo( obj->getTemplate() ) ||
2866+
if ( ( typeCountData->type && typeCountData->type->isEquivalentTo( obj->getTemplate() ) ) ||
28672867
( typeCountData->linkKey != NAMEKEY_INVALID && obj->getTemplate() != NULL && typeCountData->linkKey == obj->getTemplate()->getMaxSimultaneousLinkKey() ) )
28682868
{
28692869
typeCountData->count++;

GeneralsMD/Code/GameEngine/Source/Common/RTS/ScoreKeeper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Int ScoreKeeper::getTotalObjectsBuilt( const ThingTemplate *pTemplate )
174174
for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
175175
{
176176
const ThingTemplate *theTemplate = it->first;
177-
if (theTemplate->isEquivalentTo(pTemplate))
177+
if (theTemplate && theTemplate->isEquivalentTo(pTemplate))
178178
++count;
179179
}
180180
return count;

GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,9 +1271,8 @@ Bool BuildAssistant::isPossibleToMakeUnit( Object *builder, const ThingTemplate
12711271
// get this button
12721272
commandButton = commandSet->getCommandButton(i);
12731273
if( commandButton &&
1274-
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD ||
1275-
commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
1276-
commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
1274+
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD || commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
1275+
commandButton->getThingTemplate() && commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
12771276
foundCommand = commandButton;
12781277

12791278
} // end for i

0 commit comments

Comments
 (0)