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(gameengine): the copies that never wrote a terminator

Olcay Seygan54935032d5da
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
index b565a6c72..c9dfad927 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
@@ -292,8 +292,8 @@ void RecorderClass::cleanUpReplayFile( void )
if (TheGlobalData->m_saveStats)
{
char fname[_MAX_PATH+1];
- strncpy(fname, TheGlobalData->m_baseStatsDir.str(), _MAX_PATH);
- strncat(fname, m_fileName.str(), _MAX_PATH - strlen(fname));
+ strlcpy(fname, TheGlobalData->m_baseStatsDir.str(), ARRAY_SIZE(fname));
+ strlcat(fname, m_fileName.str(), ARRAY_SIZE(fname));
DEBUG_LOG(("Saving replay to %s\n", fname));
AsciiString oldFname;
oldFname.format("%s%s", getReplayDir().str(), m_fileName.str());
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingTemplate.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingTemplate.cpp
index 38f45ac44..18dc7d650 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingTemplate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingTemplate.cpp
@@ -1321,7 +1321,9 @@ void ThingTemplate::initForLTA(const AsciiString& name)
m_nameString = name;
char buffer[1024];
- strncpy(buffer, name.str(), sizeof(buffer));
+ // strncpy leaves no terminator when the source fills the buffer, and the loop below reads
+ // until it finds one. A 1024 character template name would have walked off the end.
+ strlcpy(buffer, name.str(), ARRAY_SIZE(buffer));
// i indexes buffer after the loop: it is the character past the first '/', or
// the terminator when the name has none.
int i;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
index a7ee326c4..b9b543b63 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
@@ -638,7 +638,9 @@ void LANAPI::RequestGameJoin( LANGameInfo *game, UnsignedInt ip /* = 0 */ )
AsciiString s = "";
GetStringFromRegistry("\\ergc", "", s);
- strncpy(msg.GameToJoin.serial, s.str(), g_maxSerialLength);
+ // strncpy with the field's own length leaves it unterminated whenever the source fills it,
+ // and the far end reads these as C strings out of the packet.
+ strlcpy(msg.GameToJoin.serial, s.str(), ARRAY_SIZE(msg.GameToJoin.serial));
msg.GameToJoin.serial[g_maxSerialLength-1] = '\0';
sendMessage(&msg, ip);
@@ -667,8 +669,10 @@ void LANAPI::RequestGameJoinDirectConnect(UnsignedInt ipaddress)
msg.LANMessageType = LANMessage::MSG_REQUEST_GAME_INFO;
fillInLANMessage(&msg);
msg.PlayerInfo.ip = GetLocalIP();
- wcsncpy(msg.PlayerInfo.playerName, m_name.str(), m_name.getLength());
- msg.PlayerInfo.playerName[m_name.getLength()] = 0;
+ // Every other copy in this file is bounded by the field, this one was bounded by the source:
+ // a name longer than the field ran the copy and then the terminator past the end of it.
+ wcsncpy(msg.PlayerInfo.playerName, m_name.str(), g_lanPlayerNameLength);
+ msg.PlayerInfo.playerName[g_lanPlayerNameLength] = 0;
sendMessage(&msg, ipaddress);
@@ -713,7 +717,7 @@ void LANAPI::RequestGameAnnounce( void )
reply.LANMessageType = LANMessage::MSG_GAME_ANNOUNCE;
AsciiString gameOpts = GameInfoToAsciiString(m_currentGame);
- strncpy(reply.GameInfo.options,gameOpts.str(),m_lanMaxOptionsLength);
+ strlcpy(reply.GameInfo.options,gameOpts.str(),ARRAY_SIZE(reply.GameInfo.options));
wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
reply.GameInfo.gameName[g_lanGameNameLength] = 0;
reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
@@ -845,7 +849,7 @@ void LANAPI::RequestGameOptions( AsciiString gameOptions, Bool isPublic, Unsigne
LANMessage msg;
fillInLANMessage( &msg );
msg.LANMessageType = LANMessage::MSG_GAME_OPTIONS;
- strncpy(msg.GameOptions.options, gameOptions.str(), m_lanMaxOptionsLength);
+ strlcpy(msg.GameOptions.options, gameOptions.str(), ARRAY_SIZE(msg.GameOptions.options));
msg.GameOptions.options[m_lanMaxOptionsLength] = 0;
sendMessage(&msg, ip);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp
index 994971830..3dd74d600 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp
@@ -64,7 +64,7 @@ void LANAPI::handleRequestLocations( LANMessage *msg, UnsignedInt senderIP )
fillInLANMessage( &reply );
reply.LANMessageType = LANMessage::MSG_GAME_ANNOUNCE;
AsciiString gameOpts = GenerateGameOptionsString();
- strncpy(reply.GameInfo.options,gameOpts.str(),m_lanMaxOptionsLength);
+ strlcpy(reply.GameInfo.options,gameOpts.str(),ARRAY_SIZE(reply.GameInfo.options));
wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
reply.GameInfo.gameName[g_lanGameNameLength] = 0;
reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
@@ -196,7 +196,7 @@ void LANAPI::handleRequestGameInfo( LANMessage *msg, UnsignedInt senderIP )
reply.LANMessageType = LANMessage::MSG_GAME_ANNOUNCE;
AsciiString gameOpts = GameInfoToAsciiString(m_currentGame);
- strncpy(reply.GameInfo.options,gameOpts.str(),m_lanMaxOptionsLength);
+ strlcpy(reply.GameInfo.options,gameOpts.str(),ARRAY_SIZE(reply.GameInfo.options));
wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
reply.GameInfo.gameName[g_lanGameNameLength] = 0;
reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DFileSystem.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DFileSystem.cpp
index aa01b17da..b41dc411e 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DFileSystem.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DFileSystem.cpp
@@ -144,8 +144,9 @@ char const * GameFileClass::Set_Name( char const *filename )
if( Is_Open() )
Close();
- // save the filename
- strncpy( m_filename, filename, _MAX_PATH );
+ // save the filename. strncpy with the full size leaves it unterminated when the name is
+ // exactly that long, and everything downstream reads m_filename as a C string.
+ strlcpy( m_filename, filename, ARRAY_SIZE(m_filename) );
char name[_MAX_PATH];
const Int EXT_LEN = 32;