Skip to content

[GEN][ZH] Fix undefined behavior in ThingTemplate::isEquivalentTo #932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,9 @@ class PartitionFilterThing : public PartitionFilter
Bool m_match;

public:
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {}
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {
DEBUG_ASSERTCRASH(m_tThing != NULL, ("ThingTemplate for PartitionFilterThing is NULL"));
}
protected:
virtual Bool allow( Object *other );
#if defined(RTS_DEBUG)
Expand Down
2 changes: 1 addition & 1 deletion Generals/Code/GameEngine/Source/Common/RTS/ScoreKeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Int ScoreKeeper::getTotalObjectsBuilt( const ThingTemplate *pTemplate )
for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
{
const ThingTemplate *theTemplate = it->first;
if (theTemplate->isEquivalentTo(pTemplate))
if (theTemplate && theTemplate->isEquivalentTo(pTemplate))
++count;
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1218,9 +1218,8 @@ Bool BuildAssistant::isPossibleToMakeUnit( Object *builder, const ThingTemplate
// get this button
commandButton = commandSet->getCommandButton(i);
if( commandButton &&
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD ||
commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD || commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
commandButton->getThingTemplate() && commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
foundCommand = commandButton;

} // end for i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ const AudioEventRTS *ThingTemplate::getPerUnitSound(const AsciiString& soundName
Bool ThingTemplate::isEquivalentTo(const ThingTemplate* tt) const
{
// sanity
if (!(this && tt))
if (!tt)
return false;

// sanity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void AISkirmishPlayer::processBaseBuilding( void )
}
}
}
if (powerPlan && powerInfo && !powerPlan->isEquivalentTo(bldgPlan)) {
if (powerInfo && powerPlan && !powerPlan->isEquivalentTo(bldgPlan)) {
if (!powerUnderConstruction) {
bldgPlan = powerPlan;
bldgInfo = powerInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
if (beacon)
{
const ThingTemplate *thing = TheThingFactory->findTemplate( beacon->getControllingPlayer()->getPlayerTemplate()->getBeaconTemplate() );
if (thing->isEquivalentTo(beacon->getTemplate()))
if (thing && thing->isEquivalentTo(beacon->getTemplate()))
{
if (beacon->getControllingPlayer() == thisPlayer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,9 @@ class PartitionFilterThing : public PartitionFilter
Bool m_match;

public:
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {}
PartitionFilterThing(const ThingTemplate *thing, Bool match) : m_tThing(thing), m_match(match) {
DEBUG_ASSERTCRASH(m_tThing != NULL, ("ThingTemplate for PartitionFilterThing is NULL"));
}
protected:
virtual Bool allow( Object *other );
#if defined(RTS_DEBUG)
Expand Down
8 changes: 4 additions & 4 deletions GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ static void doFindMostReadyWeaponForThing( Object *obj, void *userData )
return;
}

if( info->thing->isEquivalentTo( obj->getTemplate() ) )
if( info->thing && info->thing->isEquivalentTo( obj->getTemplate() ) )
{
if( !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION )
&& !obj->testStatus( OBJECT_STATUS_SOLD )
Expand Down Expand Up @@ -1409,7 +1409,7 @@ static void doFindMostReadySpecialPowerForThing( Object *obj, void *userData )
return;
}

if( info->thing->isEquivalentTo( obj->getTemplate() ) )
if( info->thing && info->thing->isEquivalentTo( obj->getTemplate() ) )
{
if( !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION )
&& !obj->testStatus( OBJECT_STATUS_SOLD )
Expand Down Expand Up @@ -1445,7 +1445,7 @@ static void doFindExistingObjectWithThingTemplate( Object *obj, void *userData )
return;
}

if( info->thing->isEquivalentTo( obj->getTemplate() ) )
if( info->thing && info->thing->isEquivalentTo( obj->getTemplate() ) )
{
if( !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION )
&& !obj->testStatus( OBJECT_STATUS_SOLD )
Expand Down Expand Up @@ -2863,7 +2863,7 @@ static void countExisting( Object *obj, void *userData )
TypeCountData *typeCountData = (TypeCountData *)userData;

// Compare templates
if ( typeCountData->type->isEquivalentTo( obj->getTemplate() ) ||
if ( ( typeCountData->type && typeCountData->type->isEquivalentTo( obj->getTemplate() ) ) ||
( typeCountData->linkKey != NAMEKEY_INVALID && obj->getTemplate() != NULL && typeCountData->linkKey == obj->getTemplate()->getMaxSimultaneousLinkKey() ) )
{
typeCountData->count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Int ScoreKeeper::getTotalObjectsBuilt( const ThingTemplate *pTemplate )
for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
{
const ThingTemplate *theTemplate = it->first;
if (theTemplate->isEquivalentTo(pTemplate))
if (theTemplate && theTemplate->isEquivalentTo(pTemplate))
++count;
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,8 @@ Bool BuildAssistant::isPossibleToMakeUnit( Object *builder, const ThingTemplate
// get this button
commandButton = commandSet->getCommandButton(i);
if( commandButton &&
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD ||
commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
(commandButton->getCommandType() == GUI_COMMAND_UNIT_BUILD || commandButton->getCommandType() == GUI_COMMAND_DOZER_CONSTRUCT) &&
commandButton->getThingTemplate() && commandButton->getThingTemplate()->isEquivalentTo(whatToBuild) )
foundCommand = commandButton;

} // end for i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ UnsignedInt ThingTemplate::getMaxSimultaneousOfType() const
Bool ThingTemplate::isEquivalentTo(const ThingTemplate* tt) const
{
// sanity
if (!(this && tt))
if (!tt)
return false;

// sanity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void AISkirmishPlayer::processBaseBuilding( void )
}
}
}
if (powerPlan && powerInfo && !powerPlan->isEquivalentTo(bldgPlan)) {
if (powerInfo && powerPlan && !powerPlan->isEquivalentTo(bldgPlan)) {
if (!powerUnderConstruction) {
bldgPlan = powerPlan;
bldgInfo = powerInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Bool RiderChangeContain::isValidContainerFor(const Object* rider, Bool checkCapa
for( int i = 0; i < MAX_RIDERS; i++ )
{
const ThingTemplate *thing = TheThingFactory->findTemplate( data->m_riders[ i ].m_templateName );
if( thing->isEquivalentTo( rider->getTemplate() ) )
if( thing && thing->isEquivalentTo( rider->getTemplate() ) )
{
//We found a valid rider, so return success.
return TRUE;
Expand Down Expand Up @@ -211,7 +211,7 @@ void RiderChangeContain::onContaining( Object *rider, Bool wasSelected )
for( int i = 0; i < MAX_RIDERS; i++ )
{
const ThingTemplate *thing = TheThingFactory->findTemplate( data->m_riders[ i ].m_templateName );
if( thing->isEquivalentTo( rider->getTemplate() ) )
if( thing && thing->isEquivalentTo( rider->getTemplate() ) )
{

//This is our rider, so set the correct model condition.
Expand Down Expand Up @@ -282,7 +282,7 @@ void RiderChangeContain::onRemoving( Object *rider )
for( int i = 0; i < MAX_RIDERS; i++ )
{
const ThingTemplate *thing = TheThingFactory->findTemplate( data->m_riders[ i ].m_templateName );
if( thing->isEquivalentTo( rider->getTemplate() ) )
if( thing && thing->isEquivalentTo( rider->getTemplate() ) )
{
//This is our rider, so clear the current model condition.
bike->clearModelConditionFlags( MAKE_MODELCONDITION_MASK2( data->m_riders[ i ].m_modelConditionFlagType, MODELCONDITION_DOOR_1_CLOSING ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
if (beacon)
{
const ThingTemplate *thing = TheThingFactory->findTemplate( beacon->getControllingPlayer()->getPlayerTemplate()->getBeaconTemplate() );
if (thing->isEquivalentTo(beacon->getTemplate()))
if (thing && thing->isEquivalentTo(beacon->getTemplate()))
{
if (beacon->getControllingPlayer() == thisPlayer)
{
Expand Down
Loading