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(gamenetwork): stop a long chat line wrapping its own length byte
Olcay Seygan5bcba7f11764
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6ed9ca316..fc60bc414 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -349,6 +349,7 @@ found and fixed — EA's own, not port damage.**- A long chat message or an unusual map name could kill the process.- Nor can a map handed to you over the network, however long a name the other machine gives it.- An order too big for one packet arrives in pieces, and a piece that claims to belong outside the order is dropped instead of landing there.+- A chat line too long for a packet is dropped rather than arriving as a different, shorter line.- The window no longer goes *Not Responding* during the menu's camera moves.- Poison clouds, mine clearing, garrison kills and crew-killing weapons: all traced and fixed.- Blowing up a full transport is survivable now, in every remaining variant.diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cppindex f12544fad..33047ad46 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp@@ -39,6 +39,20 @@//#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")#endif+// A chat message's length travels as a single byte, and all eight places that pack or measure one+// simply assigned getLength() to that byte. So a 256 character message became a length of zero and+// a 300 character one became 44 - and the size estimate, the room check and the copy each wrapped+// on their own, which is how a long line of chat turned into a packet whose declared length and+// contents disagree. Clamp instead: the byte is always honest, and a message too long to fit is+// refused by the room check rather than sent mangled.+static const Int MAX_CHAT_TEXT_LENGTH = 255;++static UnsignedByte usableChatTextLength(const UnicodeString &text)+{+ const Int len = text.getLength();+ return (UnsignedByte)((len > MAX_CHAT_TEXT_LENGTH) ? MAX_CHAT_TEXT_LENGTH : len);+}+// This function assumes that all of the fields are either of default value or are// present in the raw data.NetCommandRef * NetPacket::ConstructNetCommandMsgFromRawData(UnsignedByte *data, UnsignedShort dataLength) {@@ -540,7 +554,7 @@ UnsignedInt NetPacket::GetDisconnectChatCommandSize(NetCommandMsg *msg) {++msglen; // the 'D'msglen += sizeof(UnsignedByte); // string msglength- UnsignedByte textmsglen = cmdMsg->getText().getLength();+ UnsignedByte textmsglen = usableChatTextLength(cmdMsg->getText());msglen += textmsglen * sizeof(UnsignedShort);return msglen;@@ -577,7 +591,7 @@ UnsignedInt NetPacket::GetChatCommandSize(NetCommandMsg *msg) {++msglen; // the 'D'msglen += sizeof(UnsignedByte); // string msglength- UnsignedByte textmsglen = cmdMsg->getText().getLength();+ UnsignedByte textmsglen = usableChatTextLength(cmdMsg->getText());msglen += textmsglen * sizeof(UnsignedShort);msglen += sizeof(Int); // playerMask@@ -1434,7 +1448,7 @@ void NetPacket::FillBufferWithDisconnectChatCommand(UnsignedByte *buffer, NetCombuffer[offset] = 'D';++offset;UnicodeString unitext = cmdMsg->getText();- UnsignedByte length = unitext.getLength();+ UnsignedByte length = usableChatTextLength(unitext);memcpy(buffer + offset, &length, sizeof(UnsignedByte));offset += sizeof(UnsignedByte);@@ -1535,7 +1549,7 @@ void NetPacket::FillBufferWithChatCommand(UnsignedByte *buffer, NetCommandRef *mbuffer[offset] = 'D';++offset;UnicodeString unitext = cmdMsg->getText();- UnsignedByte length = unitext.getLength();+ UnsignedByte length = usableChatTextLength(unitext);Int playerMask = cmdMsg->getPlayerMask();memcpy(buffer + offset, &length, sizeof(UnsignedByte));offset += sizeof(UnsignedByte);@@ -1969,12 +1983,14 @@ void NetPacket::setAddress(Int addr, Int port) {Bool NetPacket::addCommand(NetCommandRef *msg) {// This is where the fun begins...- NetCommandMsg *cmdMsg = msg->getCommand();-+ // The null test was written below this, after the reference had already been followed, so the+ // one call it exists to survive was the one call that crashed.if (msg == NULL) {return TRUE; // There was nothing to add, so it was successful.}+ NetCommandMsg *cmdMsg = msg->getCommand();+switch(cmdMsg->getNetCommandType()){case NETCOMMANDTYPE_GAMECOMMAND:@@ -3265,7 +3281,7 @@ Bool NetPacket::addDisconnectChatCommand(NetCommandRef *msg) {m_packet[m_packetLen] = 'D';++m_packetLen;UnicodeString unitext = cmdMsg->getText();- UnsignedByte length = unitext.getLength();+ UnsignedByte length = usableChatTextLength(unitext);memcpy(m_packet + m_packetLen, &length, sizeof(UnsignedByte));m_packetLen += sizeof(UnsignedByte);@@ -3303,7 +3319,7 @@ Bool NetPacket::isRoomForDisconnectChatMessage(NetCommandRef *msg) {++len; // the 'D'len += sizeof(UnsignedByte); // string length- UnsignedByte textLen = cmdMsg->getText().getLength();+ UnsignedByte textLen = usableChatTextLength(cmdMsg->getText());len += textLen * sizeof(UnsignedShort);if ((len + m_packetLen) > MAX_PACKET_SIZE) {return FALSE;@@ -3378,7 +3394,7 @@ Bool NetPacket::addChatCommand(NetCommandRef *msg) {m_packet[m_packetLen] = 'D';++m_packetLen;UnicodeString unitext = cmdMsg->getText();- UnsignedByte length = unitext.getLength();+ UnsignedByte length = usableChatTextLength(unitext);Int playerMask = cmdMsg->getPlayerMask();memcpy(m_packet + m_packetLen, &length, sizeof(UnsignedByte));m_packetLen += sizeof(UnsignedByte);@@ -3428,7 +3444,7 @@ Bool NetPacket::isRoomForChatMessage(NetCommandRef *msg) {++len; // the 'D'len += sizeof(UnsignedByte); // string length- UnsignedByte textLen = cmdMsg->getText().getLength();+ UnsignedByte textLen = usableChatTextLength(cmdMsg->getText());len += textLen * sizeof(UnsignedShort);len += sizeof(Int); // playerMaskif ((len + m_packetLen) > MAX_PACKET_SIZE) {diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex f2801108e..4f03fbd3d 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -4300,6 +4300,70 @@ TEST(an_overlong_announced_filename_is_truncated_instead_of_overflowing)msg->detach();}+/* A chat message's length goes on the wire in one byte, and every site that packed or measured one+ assigned the character count straight to it. 256 characters became a length of zero; 300 became+ 44 - and the size estimate, the room check and the copy each wrapped separately, so what went+ out declared one length and carried another. */+TEST(an_overlong_chat_line_is_refused_rather_than_wrapped_to_a_short_one)+{+ NetChatCommandMsg *chat = newInstance( NetChatCommandMsg );+ UnicodeString text;+ Int k;+ for( k = 0; k < 300; ++k )+ text.concat( (WideChar)(L'a' + (k % 26)) );+ chat->setText( text );+ chat->setPlayerMask( 0xFF );+ chat->setPlayerID( 1 );+ chat->setID( 1 );+ chat->setExecutionFrame( 0 );+ chat->setNetCommandType( NETCOMMANDTYPE_CHAT );++ NetCommandRef *ref = newInstance( NetCommandRef )( chat );+ ref->setRelay( 0 );++ NetPacket *packet = newInstance( NetPacket );+ packet->init();++ /* 300 characters cannot fit a 476 byte packet, so the only right answer is to refuse it. With+ the wrapped byte the packet believed it needed 88 bytes and took the message, then wrote a+ length of 44 in front of it. */+ CHECK( !packet->addCommand( ref ) );+ CHECK_EQ( packet->getNumCommands(), 0 );++ /* and a chat line that does fit is still taken */+ NetChatCommandMsg *shortChat = newInstance( NetChatCommandMsg );+ shortChat->setText( UnicodeString( L"gg" ) );+ shortChat->setPlayerMask( 0xFF );+ shortChat->setPlayerID( 1 );+ shortChat->setID( 1 );+ shortChat->setExecutionFrame( 0 );+ shortChat->setNetCommandType( NETCOMMANDTYPE_CHAT );+ NetCommandRef *shortRef = newInstance( NetCommandRef )( shortChat );+ shortRef->setRelay( 0 );++ CHECK( packet->addCommand( shortRef ) );+ CHECK_EQ( packet->getNumCommands(), 1 );++ packet->deleteInstance();+ shortRef->deleteInstance();+ ref->deleteInstance();+ shortChat->detach();+ chat->detach();+}++/* addCommand's own guard against a null reference sat below the line that followed it, so the one+ call it exists to survive was the one call that crashed. */+TEST(adding_a_null_command_to_a_packet_is_refused_not_followed)+{+ NetPacket *packet = newInstance( NetPacket );+ packet->init();++ CHECK( packet->addCommand( NULL ) ); // nothing to add, and nothing to dereference+ CHECK_EQ( packet->getNumCommands(), 0 );++ packet->deleteInstance();+}+/* A command too big for one packet is sent as numbered chunks and reassembled into one heap blocksized from the first chunk's claim. Each later chunk then said where it went and how long itwas, and neither number was checked before the memcpy - so a peer could place its bytes anywhere