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): bound the map path and file print buffers

Olcay Seygan82ca925be134
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/File.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/File.cpp
index 1418f885d..93531b240 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/File.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/File.cpp
@@ -241,10 +241,13 @@ Bool File::print ( const Char *format, ...)
va_list args;
va_start( args, format ); /* Initialize variable arguments. */
- len = vsprintf( buffer, format, args );
+ // bounded: this was a vsprintf, so the length test below only ran AFTER the overflow had
+ // already happened. _vsnprintf returns negative when it truncates.
+ len = _vsnprintf( buffer, sizeof(buffer), format, args );
va_end( args );
+ buffer[sizeof(buffer) - 1] = 0;
- if ( len >= sizeof(buffer) )
+ if ( len < 0 || len >= sizeof(buffer) )
{
// Big Problem
assert( FALSE );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp
index 052e47a85..57ca8d33c 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp
@@ -96,7 +96,9 @@ static UnsignedInt calcCRC( AsciiString dirName, AsciiString fname )
char tempBuf[_MAX_PATH];
char filenameBuf[_MAX_PATH];
int length = 0;
- strcpy(tempBuf, fname.str());
+ // bounded: fname comes from a directory scan / the map cache and can be longer than _MAX_PATH
+ strncpy(tempBuf, fname.str(), _MAX_PATH - 1);
+ tempBuf[_MAX_PATH - 1] = 0;
length = strlen( tempBuf );
if( length >= 4 )
{
@@ -242,7 +244,9 @@ static Bool loadMap( AsciiString filename )
AsciiString asciiFile;
int length = 0;
- strcpy(tempBuf, filename.str());
+ // bounded, see above
+ strncpy(tempBuf, filename.str(), _MAX_PATH - 1);
+ tempBuf[_MAX_PATH - 1] = 0;
length = strlen( tempBuf );
if( length >= 4 )
@@ -609,7 +613,9 @@ Bool MapCache::loadUserMaps()
{
if (TheFileSystem->getFileInfo(tempfilename, &fileInfo)) {
char funk[_MAX_PATH];
- strcpy(funk, tempfilename.str());
+ // bounded, see above
+ strncpy(funk, tempfilename.str(), _MAX_PATH - 1);
+ funk[_MAX_PATH - 1] = 0;
char *filenameptr = funk;
char *tempchar = funk;
while (*tempchar != 0) {
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index 91d18bc9b..563d388ae 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -2399,8 +2399,13 @@ void GameLogic::loadMapINI( AsciiString mapName )
char filename[_MAX_PATH];
char fullFledgeFilename[_MAX_PATH];
+ //
+ // bounded copies: mapName arrives from the lobby, the network or a replay file, and an
+ // over-long path used to run straight off the end of these _MAX_PATH buffers.
+ //
memset(filename, 0, _MAX_PATH);
- strcpy(filename, mapName.str());
+ strncpy(filename, mapName.str(), _MAX_PATH - 1);
+ filename[_MAX_PATH - 1] = 0;
//
// if map name begins with a "SAVE_DIRECTORY\", then the map refers to a map
@@ -2409,7 +2414,10 @@ void GameLogic::loadMapINI( AsciiString mapName )
// for that map from it's original location
//
if (TheGameState->isInSaveDirectory(filename))
- strcpy( filename, TheGameState->getSaveGameInfo()->pristineMapName.str() );
+ {
+ strncpy( filename, TheGameState->getSaveGameInfo()->pristineMapName.str(), _MAX_PATH - 1 );
+ filename[_MAX_PATH - 1] = 0;
+ }
// sanity
int length = strlen(filename);
@@ -2425,14 +2433,14 @@ void GameLogic::loadMapINI( AsciiString mapName )
*extension = 0;
- sprintf(fullFledgeFilename, "%s\\map.ini", filename);
+ _snprintf(fullFledgeFilename, _MAX_PATH, "%s\\map.ini", filename); fullFledgeFilename[_MAX_PATH-1] = 0;
if (TheFileSystem->doesFileExist(fullFledgeFilename)) {
DEBUG_LOG(("Loading map.ini\n"));
INI ini;
ini.load( AsciiString(fullFledgeFilename), INI_LOAD_CREATE_OVERRIDES, NULL );
}
- sprintf(fullFledgeFilename, "%s\\solo.ini", filename);
+ _snprintf(fullFledgeFilename, _MAX_PATH, "%s\\solo.ini", filename); fullFledgeFilename[_MAX_PATH-1] = 0;
if (TheFileSystem->doesFileExist(fullFledgeFilename)) {
DEBUG_LOG(("Loading solo.ini\n"));
INI ini;
@@ -2442,7 +2450,7 @@ void GameLogic::loadMapINI( AsciiString mapName )
// No error here. There could've just *not* been a map.ini file.
// now look for a string file
- sprintf(fullFledgeFilename, "%s\\map.str", filename);
+ _snprintf(fullFledgeFilename, _MAX_PATH, "%s\\map.str", filename); fullFledgeFilename[_MAX_PATH-1] = 0;
if (TheFileSystem->doesFileExist(fullFledgeFilename)) {
TheGameText->initMapStringFile(fullFledgeFilename);
@@ -2452,7 +2460,7 @@ void GameLogic::loadMapINI( AsciiString mapName )
if (TheDisplay)
{
const char* ASSET_USAGE_FILE_NAME = "AssetUsage.txt";
- sprintf(fullFledgeFilename, "%s\\%s", filename, ASSET_USAGE_FILE_NAME);
+ _snprintf(fullFledgeFilename, _MAX_PATH, "%s\\%s", filename, ASSET_USAGE_FILE_NAME); fullFledgeFilename[_MAX_PATH-1] = 0;
// note: call this EVEN IF THE FILE IN QUESTION DOES NOT EXIST.
TheDisplay->doSmartAssetPurgeAndPreload(fullFledgeFilename);
}