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(gamenetwork): clamp server-fed seat side and colour, read testMode

Olcay Seyganad7b3527c784
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ffdff6f25..627651861 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -948,6 +948,7 @@ found and fixed, EA's own, not port damage.**
- A desync now writes a per-object report on both machines for comparison.
- LAN refuses to start between two machines whose game files differ.
- A player whose name holds a comma, a colon or a control character is turned away at the door instead of scrambling the lobby's idea of who is in the room.
+- A Quick Match can no longer crash your game as it starts. A server that sent colour 999, or an army number past the end of the list, took the game down on the loading screen; now that seat reads as Random.
- An open seat can be taken. The map's number of starting positions used to overrule the host: a four position map turned away the fifth player even with a seat left open for him, and that seat stayed open on everyone's screen.
- A map sent to you over the network has to be a map. The other machine used to name any file it liked and fill it with anything at all, and it was written where the name pointed; now the name cannot leave the map folder, the kind has to be one a map transfer carries, and the contents have to match the kind.
- The processor's rounding mode is reset from the right register every frame.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineQueues.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineQueues.h
index 646afd459..8792f4ef9 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineQueues.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineQueues.h
@@ -65,6 +65,9 @@ OnlineStatus OnlineGetStatus( void );
AsciiString OnlineGetMOTD( void ); ///< welcome.motd, for SetUpGameSpy's motdBuffer
AsciiString OnlineGetConfig( void ); ///< welcome.config (<QMMaps>), for SetUpGameSpy's configBuffer
void OnlineDisconnect( void ); ///< hangs up; the next OnlineUpdate dials again
+/* The welcome said testMode, which a server sends only when started for a test. Only then does the
+ -control socket run world commands in an online match: anywhere else they are a cheat. */
+Bool OnlineIsTestServer( void );
/* The relay. Both are FALSE until a login succeeded (auth.ok). The ticket goes into the UDP HELLO_AUTH,
which has to be sent right after login: the peer queue answers PEERREQUEST_LOGIN only once the server
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineSession.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineSession.h
index 12b7475b1..02dcd433b 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineSession.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Online/OnlineSession.h
@@ -126,6 +126,7 @@ struct OnlineSession
std::string motd; ///< std::string, not AsciiString: this outlives the engine's allocator
std::string config;
UnsignedShort relayPort;
+ Bool testMode; ///< welcome.testMode: OnlineIsTestServer
// the account
Bool authed; ///< auth.ok arrived
@@ -172,4 +173,11 @@ private:
/* Bytes a server line's number field holds, for vips and ids. */
UnsignedInt OnlineHostToNet( UnsignedInt v );
+/* A side or colour the server hands a seat (qm.matched, the game list), as the seat may take it. A side
+ is a faction below numTemplates, Random or a Random pool; a colour is below numColors. Anything else,
+ the observer included, is Random (-1): GameLogic dereferences the template and the colour a seat
+ names, so a hostile server's 999 was a crash at match start. */
+Int OnlineSeatSide( long long side, Int numTemplates );
+Int OnlineSeatColor( long long color, Int numColors );
+
#endif // _ONLINE_SESSION_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Online/OnlinePeer.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Online/OnlinePeer.cpp
index ea1df57ee..274d77a2d 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Online/OnlinePeer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Online/OnlinePeer.cpp
@@ -25,12 +25,16 @@
#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
#include "Common/GlobalData.h"
+#include "Common/MultiplayerSettings.h"
+#include "Common/PlayerTemplate.h"
#include "Common/UserPreferences.h"
#include "Common/version.h"
+#include "GameNetwork/GameInfo.h"
#include "GameNetwork/GameSpy/ThreadUtils.h"
#include "GameNetwork/Online/OnlineSession.h"
#include "zhio.h"
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -83,6 +87,29 @@ static PeerResponse makePeer( Int type )
return r;
}
+Int OnlineSeatSide( long long side, Int numTemplates )
+{
+ if (side >= 0)
+ return side < numTemplates ? (Int)side : (Int)PLAYERTEMPLATE_RANDOM;
+ // below the pool marker IsValidPlayerTemplatePick reads only the mask, never the store
+ return side < PLAYERTEMPLATE_RANDOM_POOL && side >= INT_MIN && IsValidPlayerTemplatePick( (Int)side ) ? (Int)side : (Int)PLAYERTEMPLATE_RANDOM;
+}
+
+Int OnlineSeatColor( long long color, Int numColors )
+{
+ return color >= 0 && color < numColors ? (Int)color : -1;
+}
+
+static Int seatSide( long long side )
+{
+ return OnlineSeatSide( side, ThePlayerTemplateStore ? ThePlayerTemplateStore->getPlayerTemplateCount() : 0 );
+}
+
+static Int seatColor( long long color )
+{
+ return OnlineSeatColor( color, TheMultiplayerSettings ? TheMultiplayerSettings->getNumColors() : 0 );
+}
+
//-------------------------------------------------------------------------------------------------
// the session
//-------------------------------------------------------------------------------------------------
@@ -94,7 +121,7 @@ OnlineSession &OnlineSession::get()
}
OnlineSession::OnlineSession() :
- connection( NULL ), capture( NULL ), status( ONLINE_OFF ), relayPort( 0 ),
+ connection( NULL ), capture( NULL ), status( ONLINE_OFF ), relayPort( 0 ), testMode( FALSE ),
peer( NULL ), buddy( NULL ), ps( NULL )
{
forgetAccount();
@@ -133,6 +160,7 @@ void OnlineSession::beginTest( std::vector<OnlineMessage> *lines )
motd.clear();
config.clear();
relayPort = 8094;
+ testMode = FALSE;
status = lines ? ONLINE_WELCOMED : ONLINE_OFF;
}
@@ -366,8 +394,8 @@ void OnlineSession::gameListEntry( const OnlineMessage &m )
if (i < (Int)wins.size()) r.stagingRoom.wins[i] = (Int)wins[i];
if (i < (Int)losses.size()) r.stagingRoom.losses[i] = (Int)losses[i];
if (i < (Int)pids.size()) r.stagingRoom.profileID[i] = (Int)pids[i];
- if (i < (Int)factions.size()) r.stagingRoom.faction[i] = (Int)factions[i];
- if (i < (Int)colors.size()) r.stagingRoom.color[i] = (Int)colors[i];
+ if (i < (Int)factions.size()) r.stagingRoom.faction[i] = seatSide( factions[i] );
+ if (i < (Int)colors.size()) r.stagingRoom.color[i] = seatColor( colors[i] );
}
}
peerResponse( r );
@@ -409,6 +437,7 @@ void OnlineSession::dispatch( const OnlineMessage &m )
motd = m.getString( "motd" );
config = m.getString( "config" );
relayPort = (UnsignedShort)m.getInt( "relayPort" );
+ testMode = m.getBool( "testMode" );
status = ONLINE_WELCOMED;
if (hasPendingAuth)
{
@@ -658,8 +687,8 @@ void OnlineSession::dispatch( const OnlineMessage &m )
{
r.stagingRoomPlayerNames[i] = nicks[i];
r.qmStatus.IP[i] = i < v.size() ? (UnsignedInt)v[i] : 0; // host order, as GameSpy's matchbot sent it
- r.qmStatus.side[i] = i < sides.size() ? (Int)sides[i] : -1;
- r.qmStatus.color[i] = i < colors.size() ? (Int)colors[i] : -1;
+ r.qmStatus.side[i] = i < sides.size() ? seatSide( sides[i] ) : -1;
+ r.qmStatus.color[i] = i < colors.size() ? seatColor( colors[i] ) : -1;
r.qmStatus.nat[i] = ONLINE_NAT_SIMPLE;
}
peerResponse( r );
@@ -985,6 +1014,11 @@ void OnlineDisconnect( void )
OnlineSession::get().disconnect();
}
+Bool OnlineIsTestServer( void )
+{
+ return OnlineIsEnabled() && OnlineSession::get().testMode;
+}
+
Bool OnlineGetRelayTicket( UnsignedByte out[16] )
{
OnlineSession &s = OnlineSession::get();
diff --git a/GeneralsMD/Code/Tests/test_online.cpp b/GeneralsMD/Code/Tests/test_online.cpp
index 00f458b41..0c475fbcf 100644
--- a/GeneralsMD/Code/Tests/test_online.cpp
+++ b/GeneralsMD/Code/Tests/test_online.cpp
@@ -21,6 +21,7 @@
#include <map>
#include "GameNetwork/Online/OnlineConnection.h"
#include "GameNetwork/Online/OnlineSession.h"
+#include "GameNetwork/GameInfo.h"
#include <chrono>
#include <string>
@@ -1050,7 +1051,9 @@ TEST(online_adapter_quick_match)
widen.peerRequestType = PeerRequest::PEERREQUEST_WIDENQUICKMATCHSEARCH;
rig.peer->addRequest( widen );
CHECK_STR( rig.sentType( 1 ).c_str(), "qm.widen" );
- rig.feed( "{\"t\":\"qm.matched\",\"match\":\"q1\",\"seed\":77,\"mapIdx\":1,\"nicks\":[\"Tank\",\"Kim\"],\"ids\":[7,9],\"vips\":[167772163,167772162],\"sides\":[2,-1],\"colors\":[3,-1]}" );
+ // side -25 is a Random pool (America and Turkiye): this binary has no template store, so a pool is the
+ // one kind of side that reaches the seat unchanged here
+ rig.feed( "{\"t\":\"qm.matched\",\"match\":\"q1\",\"seed\":77,\"mapIdx\":1,\"nicks\":[\"Tank\",\"Kim\"],\"ids\":[7,9],\"vips\":[167772163,167772162],\"sides\":[-25,-1],\"colors\":[3,-1]}" );
std::vector<PeerResponse> r = rig.peerResponses();
CHECK_EQ( r.size(), (size_t)3 );
@@ -1067,13 +1070,83 @@ TEST(online_adapter_quick_match)
CHECK_STR( r[2].stagingRoomPlayerNames[1].c_str(), "Kim" );
CHECK( r[2].stagingRoomPlayerNames[2].empty() );
CHECK_EQ( r[2].qmStatus.IP[1], 0x0A000002u );
- CHECK_EQ( r[2].qmStatus.side[0], 2 );
+ CHECK_EQ( r[2].qmStatus.side[0], -25 );
CHECK_EQ( r[2].qmStatus.color[1], -1 );
CHECK_EQ( r[2].qmStatus.nat[0], 1 );
}
CHECK_STR( OnlineSession::get().matchId.c_str(), "q1" );
}
+/* GameLogic dereferences the template and the colour a seat names (getColor(999)->getColor() was a
+ crash at Quick Match start), so whatever the server says is clamped before a seat sees it. */
+TEST(online_seat_side_and_color_are_clamped)
+{
+ const Int TEMPLATES = 5, COLORS = 8;
+ CHECK_EQ( OnlineSeatSide( 0, TEMPLATES ), 0 );
+ CHECK_EQ( OnlineSeatSide( 4, TEMPLATES ), 4 );
+ CHECK_EQ( OnlineSeatSide( -1, TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( PLAYERTEMPLATE_RANDOM_POOL - 0x9, TEMPLATES ), PLAYERTEMPLATE_RANDOM_POOL - 0x9 );
+ CHECK_EQ( OnlineSeatSide( PLAYERTEMPLATE_RANDOM_POOL - 0x3FFFFFFF, TEMPLATES ), PLAYERTEMPLATE_RANDOM_POOL - 0x3FFFFFFF );
+ CHECK_EQ( OnlineSeatSide( 5, TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( 999, TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( PLAYERTEMPLATE_OBSERVER, TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( -3, TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( PLAYERTEMPLATE_RANDOM_POOL, TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( PLAYERTEMPLATE_RANDOM_POOL - 0x40000000LL, TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( (1LL << 32) + 2, TEMPLATES ), -1 ); // not 2 after a truncation
+ CHECK_EQ( OnlineSeatSide( -(1LL << 40), TEMPLATES ), -1 );
+ CHECK_EQ( OnlineSeatSide( 0, 0 ), -1 );
+
+ CHECK_EQ( OnlineSeatColor( 0, COLORS ), 0 );
+ CHECK_EQ( OnlineSeatColor( 7, COLORS ), 7 );
+ CHECK_EQ( OnlineSeatColor( -1, COLORS ), -1 );
+ CHECK_EQ( OnlineSeatColor( 8, COLORS ), -1 );
+ CHECK_EQ( OnlineSeatColor( 999, COLORS ), -1 );
+ CHECK_EQ( OnlineSeatColor( -2, COLORS ), -1 );
+ CHECK_EQ( OnlineSeatColor( (1LL << 32) + 3, COLORS ), -1 );
+
+ // through the adapter: a hostile qm.matched and a hostile game list line
+ AdapterRig rig;
+ rig.login();
+ rig.feed( "{\"t\":\"qm.matched\",\"match\":\"q9\",\"seed\":1,\"mapIdx\":-5,\"nicks\":[\"Tank\",\"Kim\"],\"ids\":[7,9],\"vips\":[1,2],\"sides\":[999,-2],\"colors\":[999,-7]}" );
+ OnlineMessage opts;
+ opts.setInts( "factions", std::vector<long long>( 2, 999 ) ).setInts( "colors", std::vector<long long>( 2, 4294967296LL ) );
+ std::string optsLine;
+ CHECK( opts.toLine( optsLine ) );
+ OnlineMessage game;
+ game.setString( "t", "game" ).setString( "action", "add" ).setInt( "id", 3 ).setString( "host", "Kim" );
+ game.setString( "opts", optsLine.substr( 0, optsLine.size() - 1 ) );
+ rig.feed( game );
+
+ std::vector<PeerResponse> r = rig.peerResponses();
+ CHECK_EQ( r.size(), (size_t)2 );
+ if (r.size() == 2)
+ {
+ CHECK_EQ( (int)r[0].qmStatus.status, (int)QM_MATCHED );
+ for (Int i = 0; i < 2; ++i)
+ {
+ CHECK_EQ( r[0].qmStatus.side[i], -1 );
+ CHECK_EQ( r[0].qmStatus.color[i], -1 );
+ CHECK_EQ( r[1].stagingRoom.faction[i], -1 );
+ CHECK_EQ( r[1].stagingRoom.color[i], -1 );
+ }
+ }
+}
+
+TEST(online_test_server_only_when_the_welcome_says_so)
+{
+ AdapterRig rig;
+ CHECK( !OnlineIsTestServer() );
+ rig.feed( "{\"t\":\"welcome\",\"config\":\"\",\"motd\":\"\",\"relayPort\":8094}" );
+ CHECK( !OnlineIsTestServer() );
+ rig.feed( "{\"t\":\"welcome\",\"config\":\"\",\"motd\":\"\",\"relayPort\":8094,\"testMode\":\"yes\"}" );
+ CHECK( !OnlineIsTestServer() ); // a bool, not anything truthy
+ rig.feed( "{\"t\":\"welcome\",\"config\":\"\",\"motd\":\"\",\"relayPort\":8094,\"testMode\":true}" );
+ CHECK( OnlineIsTestServer() );
+ rig.feed( "{\"t\":\"welcome\",\"config\":\"\",\"motd\":\"\",\"relayPort\":8094,\"testMode\":false}" );
+ CHECK( !OnlineIsTestServer() );
+}
+
TEST(online_adapter_stats_read_and_writes_dropped)
{
AdapterRig rig;