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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gameengine): drop a command whose player index names nobody
Olcay Seygana15c151951b5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3c0e410e7..7ce11df22 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -799,6 +799,13 @@ looks right to you.- **Ordering a Jarmen Kell to snipe an empty bike** — one whose rider you already shot — was acrash rather than a refusal. So was a formation move that included something which cannot driveitself, and telling a hacker to step out of the way at the exact moment it stood up.+- **An order had to say who sent it, and nobody ever checked.** Every command carries the number of+ the player who gave it, and that number arrives from the other machine or out of a replay file —+ it is not something this copy of the game works out for itself. The check that it named a real+ player was written as a debug-only assertion, which means it was not there at all in the version+ you play, and the very next thing the game does is ask that player what they have selected. A+ damaged replay, or a command from a player who had already left the game, was a crash. Now the+ order is simply dropped.And when something does go wrong, it now writes a readable crash report instead of the windowsilently vanishing.diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameCommon.h b/GeneralsMD/Code/GameEngine/Include/Common/GameCommon.hindex 63f89c593..f78362469 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/GameCommon.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameCommon.h@@ -113,6 +113,16 @@ enumMAX_PLAYER_COUNT = 16 ///< max number of Players.};+//+// A player index that arrives from outside this machine - off the wire, or out of a replay file -+// is only a number, and nothing on the way in bounds it. Everything that turns one into a Player+// asks this first; a number that fails it names nobody and the command carrying it is dropped.+//+inline Bool isValidPlayerIndex( Int playerIndex )+{+ return playerIndex >= 0 && playerIndex < MAX_PLAYER_COUNT;+}+// ----------------------------------------------------------------------------------------------/**a bitmask that can uniquely represent each player.diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cppindex 078de37ac..f2e220487 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp@@ -98,7 +98,7 @@ PlayerList::~PlayerList()//-----------------------------------------------------------------------------Player *PlayerList::getNthPlayer(Int i){- if( i < 0 || i >= MAX_PLAYER_COUNT )+ if( !isValidPlayerIndex( i ) ){// DEBUG_CRASH( ("Illegal player index\n") );return NULL;diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cppindex ff3748b81..b55790b4b 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp@@ -405,10 +405,22 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )DEBUG_ASSERTCRASH(msg != NULL && msg != (GameMessage*)0xdeadbeef, ("bad msg"));#endif+ //+ // This used to be an assertion, which is nothing at all in a release build, and the very next+ // thing the function does with a network message is thisPlayer->getCurrentSelectionAsAIGroup().+ // The index is not generated here for anything that comes from outside: a replay file supplies it+ // with a raw fread whose failure leaves the -1 it was seeded with, and a network command gets it+ // from the sending machine. An index naming nobody is a command that cannot be carried out, so+ // drop it rather than dereference the NULL.+ //Player *thisPlayer = ThePlayerList->getNthPlayer( msg->getPlayerIndex() );- DEBUG_ASSERTCRASH( thisPlayer, ("logicMessageDispatcher: Processing message from unknown player (player index '%d')\n",- msg->getPlayerIndex()) );-+ if( thisPlayer == NULL )+ {+ DEBUG_CRASH( ("logicMessageDispatcher: Processing message from unknown player (player index '%d')",+ msg->getPlayerIndex()) );+ return;+ }+AIGroup *currentlySelectedGroup = NULL;if (isInGame())@@ -2204,7 +2216,7 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )ScienceType science = (ScienceType)msg->getArgument( 0 )->integer;// sanity- if( science == SCIENCE_INVALID || thisPlayer == NULL )+ if( science == SCIENCE_INVALID )break;thisPlayer->attemptToPurchaseScience(science);diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cppindex 3e02e239c..d922f0935 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp@@ -173,10 +173,17 @@ GameMessage *NetGameCommandMsg::constructGameMessage(){GameMessage *retval = newInstance(GameMessage)(m_type);+ //+ // The player id comes off the wire, and the name it builds does not have to match anybody: a+ // command that arrives while the player list is being torn down, or one naming a slot this room+ // never filled, finds nothing. findPlayerWithNameKey returns NULL for that and the result was+ // dereferenced on the spot. Hand the message an index that names nobody instead and let the+ // logic dispatcher drop it, which is what it does with any index outside the player list.+ //AsciiString name;name.format("player%d", getPlayerID());- retval->friend_setPlayerIndex( ThePlayerList->findPlayerWithNameKey(TheNameKeyGenerator->nameToKey(name))->getPlayerIndex());-// retval->friend_setPlayerIndex(indexFromMask(ThePlayerList->findPlayerWithNameKey(TheNameKeyGenerator->nameToKey(name))->getPlayerMask()));+ Player *sender = ThePlayerList->findPlayerWithNameKey(TheNameKeyGenerator->nameToKey(name));+ retval->friend_setPlayerIndex( sender ? sender->getPlayerIndex() : -1 );GameMessageArgument *arg = m_argList;while (arg != NULL) {diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 332d9c5bf..46c7e811d 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -3721,3 +3721,27 @@ TEST(a_command_may_only_name_an_object_its_sender_controls)CHECK( !isPlayerCommandingOwnObject( NULL, alice ) );CHECK( !isPlayerCommandingOwnObject( NULL, NULL ) );}++/* A player index that arrives from outside this machine is only a number: the replay reader freads+ it over a -1 and never checks the read, and a network command carries whatever the sending+ machine put in it. getNthPlayer answers NULL for anything outside the list, and the logic+ dispatcher used to assert - i.e. do nothing in a release build - and dereference it anyway. */+TEST(a_player_index_from_outside_this_machine_is_bounded_before_use)+{+ // the whole list, and nothing either side of it+ CHECK( isValidPlayerIndex( 0 ) );+ CHECK( isValidPlayerIndex( MAX_PLAYER_COUNT - 1 ) );+ CHECK( !isValidPlayerIndex( MAX_PLAYER_COUNT ) );+ CHECK( !isValidPlayerIndex( -1 ) ); // what a failed fread of a replay leaves behind++ // a truncated or hostile value off the wire+ CHECK( !isValidPlayerIndex( 0x7fffffff ) );+ CHECK( !isValidPlayerIndex( (Int)0x80000000 ) );+ CHECK( !isValidPlayerIndex( -12345 ) );++ Bool acceptedOnlyTheList = TRUE;+ for( Int i = -64; i < 64; ++i )+ if( isValidPlayerIndex( i ) != (i >= 0 && i < MAX_PLAYER_COUNT) )+ acceptedOnlyTheList = FALSE;+ CHECK( acceptedOnlyTheList );+}