Skip to content

Commit c3cc2c9

Browse files
fix: Prevent PlayerList::newGame array out-of-bounds and null dereferences
1 parent fe65574 commit c3cc2c9

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ void PlayerList::newGame()
139139
if (pname.isEmpty())
140140
continue; // it's neutral, which we've already done, so skip it.
141141

142+
if (m_playerCount >= MAX_PLAYER_COUNT)
143+
{
144+
DEBUG_ASSERTCRASH(false, ("Map has more player sides than MAX_PLAYER_COUNT (%d); skipping side '%s'", MAX_PLAYER_COUNT, pname.str()));
145+
continue;
146+
}
147+
142148
/// @todo The Player class should have a reset() method, instead of directly calling initFromDict() (MSB)
143149
Player* p = m_players[m_playerCount++];
144150
p->initFromDict(d);
@@ -188,6 +194,9 @@ void PlayerList::newGame()
188194
Dict *d = TheSidesList->getSideInfo(i)->getDict();
189195
Player* p = findPlayerWithNameKey(NAMEKEY(d->getAsciiString(TheKey_playerName)));
190196

197+
if (!p)
198+
continue; // player was skipped (e.g. side count exceeded MAX_PLAYER_COUNT) or is neutral with no name key.
199+
191200
AsciiString tok;
192201

193202
AsciiString enemies = d->getAsciiString(TheKey_playerEnemies);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ void PlayerList::newGame()
139139
if (pname.isEmpty())
140140
continue; // it's neutral, which we've already done, so skip it.
141141

142+
if (m_playerCount >= MAX_PLAYER_COUNT)
143+
{
144+
DEBUG_ASSERTCRASH(false, ("Map has more player sides than MAX_PLAYER_COUNT (%d); skipping side '%s'", MAX_PLAYER_COUNT, pname.str()));
145+
continue;
146+
}
147+
142148
/// @todo The Player class should have a reset() method, instead of directly calling initFromDict() (MSB)
143149
Player* p = m_players[m_playerCount++];
144150
p->initFromDict(d);
@@ -188,6 +194,9 @@ void PlayerList::newGame()
188194
Dict *d = TheSidesList->getSideInfo(i)->getDict();
189195
Player* p = findPlayerWithNameKey(NAMEKEY(d->getAsciiString(TheKey_playerName)));
190196

197+
if (!p)
198+
continue; // player was skipped (e.g. side count exceeded MAX_PLAYER_COUNT) or is neutral with no name key.
199+
191200
AsciiString tok;
192201

193202
AsciiString enemies = d->getAsciiString(TheKey_playerEnemies);

0 commit comments

Comments
 (0)