Skip to content

Commit f8d2336

Browse files
authored
refactor: Remove superfluous string buffer copies (#1800)
1 parent ff65d33 commit f8d2336

File tree

33 files changed

+74
-249
lines changed

33 files changed

+74
-249
lines changed

Generals/Code/GameEngine/Source/Common/System/SaveGame/GameState.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,13 @@ static AsciiString getMapLeafAndDirName(const AsciiString& in)
847847
// ------------------------------------------------------------------------------------------------
848848
static AsciiString removeExtension(const AsciiString& in)
849849
{
850-
char buf[1024];
851-
strcpy(buf, in.str());
852-
char* p = strrchr(buf, '.');
853-
if (p)
850+
if (const char* end = in.reverseFind('.'))
854851
{
855-
*p = 0;
852+
const char* begin = in.str();
853+
return AsciiString(begin, end - begin);
856854
}
857-
return AsciiString(buf);
855+
856+
return in;
858857
}
859858

860859
// ------------------------------------------------------------------------------------------------
@@ -1619,22 +1618,16 @@ void GameState::xfer( Xfer *xfer )
16191618
saveGameInfo->mapLabel = dict->getAsciiString( TheKey_mapName, &exists );
16201619

16211620
// if no label was found, we'll use the map name (just filename, no directory info)
1622-
if( exists == FALSE || saveGameInfo->mapLabel == AsciiString::TheEmptyString )
1621+
if (exists == FALSE || saveGameInfo->mapLabel == AsciiString::TheEmptyString)
16231622
{
1624-
char string[ _MAX_PATH ];
1625-
1626-
strcpy( string, TheGlobalData->m_mapName.str() );
1627-
char *p = strrchr( string, '\\' );
1628-
if( p == NULL )
1623+
const char* p = TheGlobalData->m_mapName.reverseFind('\\');
1624+
if (p == NULL)
16291625
saveGameInfo->mapLabel = TheGlobalData->m_mapName;
16301626
else
16311627
{
1632-
16331628
p++; // skip the '\' we're on
1634-
saveGameInfo->mapLabel.set( p );
1635-
1629+
saveGameInfo->mapLabel.set(p);
16361630
}
1637-
16381631
}
16391632

16401633
// xfer map label

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/NetworkDirectConnect.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,9 @@ void JoinDirectConnectGame()
219219
AsciiString ipstring;
220220
asciientry.nextToken(&ipstring, "(");
221221

222-
char ipstr[16];
223-
strcpy(ipstr, ipstring.str());
224-
225222
Int ip1, ip2, ip3, ip4;
226-
sscanf(ipstr, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
223+
Int numFields = sscanf(ipstring.str(), "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
224+
(void)numFields; DEBUG_ASSERTCRASH(numFields == 4, ("JoinDirectConnectGame - invalid IP address format: %s", ipstring.str()));
227225

228226
DEBUG_LOG(("JoinDirectConnectGame - joining at %d.%d.%d.%d", ip1, ip2, ip3, ip4));
229227

Generals/Code/GameEngine/Source/GameLogic/ScriptEngine/ScriptActions.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,11 +2550,10 @@ void ScriptActions::doDisplayCinematicText(const AsciiString& displayText, const
25502550

25512551
// get the font name
25522552
AsciiString fontName = AsciiString::TheEmptyString;
2553-
char buf[256];
2554-
char *c;
2555-
strcpy(buf, fontType.str());
2553+
25562554
// TheSuperHackers @fix xezon 22/03/2025 Fixes potential buffer overrun via prior c!='\0' test.
2557-
for( c = buf; *c != '\0'; c++ )
2555+
const char* c = fontType.str();
2556+
for (; *c != '\0'; c++)
25582557
{
25592558
if( *c != ' ' && *c++ != '-' )
25602559
fontName.concat(c);

Generals/Code/GameEngine/Source/GameNetwork/FirewallHelper.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,7 @@ Bool FirewallHelperClass::detectionBeginUpdate() {
679679
/*
680680
** Do the lookup.
681681
*/
682-
char temp_name[256];
683-
strcpy(temp_name, mangler_name_ptr);
684-
struct hostent *host_info = gethostbyname(temp_name);
682+
struct hostent *host_info = gethostbyname(mangler_name_ptr);
685683

686684
if (!host_info) {
687685
DEBUG_LOG(("gethostbyname failed! Error code %d", WSAGetLastError()));

Generals/Code/GameEngine/Source/GameNetwork/udp.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,12 @@ UDP::~UDP()
127127

128128
Int UDP::Bind(const char *Host,UnsignedShort port)
129129
{
130-
char hostName[100];
131130
struct hostent *hostStruct;
132131
struct in_addr *hostNode;
133132

134133
if (isdigit(Host[0]))
135134
return ( Bind( ntohl(inet_addr(Host)), port) );
136135

137-
strcpy(hostName, Host);
138-
139136
hostStruct = gethostbyname(Host);
140137
if (hostStruct == NULL)
141138
return (0);

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DFileSystem.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,12 @@ char const * GameFileClass::Set_Name( char const *filename )
232232
if( fileType == FILE_TYPE_W3D )
233233
{
234234
sprintf(m_filePath,USER_W3D_DIR_PATH, TheGlobalData->getPath_UserData().str());
235-
//strcpy( m_filePath, USER_W3D_DIR_PATH );
236235
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
237236

238237
}
239238
else if( isImageFileType(fileType) )
240239
{
241240
sprintf(m_filePath,USER_TGA_DIR_PATH, TheGlobalData->getPath_UserData().str());
242-
//strcpy( m_filePath, USER_TGA_DIR_PATH );
243241
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
244242

245243
}
@@ -256,7 +254,6 @@ char const * GameFileClass::Set_Name( char const *filename )
256254
if( fileType == FILE_TYPE_TGA ) // just TGA, since we don't do dds previews
257255
{
258256
sprintf(m_filePath,MAP_PREVIEW_DIR_PATH, TheGlobalData->getPath_UserData().str());
259-
//strcpy( m_filePath, USER_TGA_DIR_PATH );
260257
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
261258

262259
}

Generals/Code/GameEngineDevice/Source/W3DDevice/GameLogic/W3DTerrainLogic.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,6 @@ Bool W3DTerrainLogic::loadMap( AsciiString filename , Bool query )
117117

118118
WorldHeightMap *terrainHeightMap; ///< holds raw heightmap data samples
119119

120-
char tempBuf[_MAX_PATH];
121-
char filenameBuf[_MAX_PATH];
122-
int length = 0;
123-
124-
strcpy(tempBuf, filename.str());
125-
126-
length = strlen( tempBuf );
127-
if( length >= 4 )
128-
{
129-
strlcpy( filenameBuf, tempBuf, length - 4 + 1);
130-
}
131-
132-
// const char *fname = filename.reverseFind('\\');
133-
// if (fname)
134-
// filename = fname+1;
135-
136120
CachedFileInputStream fileStrm;
137121
if ( !fileStrm.open(filename) )
138122
{

Generals/Code/Tools/GUIEdit/Source/GUIEdit.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,7 +3539,6 @@ void GUIEdit::stripNameDecorations( GameWindow *root )
35393539

35403540
if( !instData->m_decoratedNameString.isEmpty() )
35413541
{
3542-
char nameOnly[ MAX_WINDOW_NAME_LEN ];
35433542
const char *c;
35443543

35453544
// skip past the "filename.wnd:" to the name only
@@ -3550,11 +3549,8 @@ void GUIEdit::stripNameDecorations( GameWindow *root )
35503549
// skip beyong the scope resolution colon
35513550
c++;
35523551

3553-
// copy the name
3554-
strcpy( nameOnly, c );
3555-
35563552
// put the name only in the decoration field
3557-
instData->m_decoratedNameString = nameOnly;
3553+
instData->m_decoratedNameString = c;
35583554

35593555
}
35603556

Generals/Code/Tools/WorldBuilder/src/BlendMaterial.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ void BlendMaterial::addTerrain(const char *pPath, Int terrainNdx, HTREEITEM pare
207207
// Int availableTiles = 4 * tilesPerRow * tilesPerRow;
208208
// Int percent = (WorldHeightMapEdit::getTexClassNumTiles(terrainNdx)*100 + availableTiles/2) / availableTiles;
209209

210-
char label[_MAX_PATH];
211-
snprintf(label, ARRAY_SIZE(label), "%s", buffer);
212-
213-
214210
if( doAdd )
215211
{
216212
TVINSERTSTRUCT ins;
@@ -220,8 +216,8 @@ void BlendMaterial::addTerrain(const char *pPath, Int terrainNdx, HTREEITEM pare
220216
ins.hInsertAfter = TVI_LAST;
221217
ins.item.mask = TVIF_PARAM|TVIF_TEXT;
222218
ins.item.lParam = terrainNdx;
223-
ins.item.pszText = label;
224-
ins.item.cchTextMax = strlen(label)+2;
219+
ins.item.pszText = buffer;
220+
ins.item.cchTextMax = strlen(buffer)+2;
225221
m_terrainTreeView.InsertItem(&ins);
226222
}
227223

Generals/Code/Tools/WorldBuilder/src/BuildList.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -737,24 +737,20 @@ void BuildList::OnExport()
737737
static FILE *theLogFile = NULL;
738738
Bool open = false;
739739
try {
740-
char dirbuf[ _MAX_PATH ];
741-
::GetModuleFileName( NULL, dirbuf, sizeof( dirbuf ) );
742-
if (char *pEnd = strrchr(dirbuf, '\\'))
740+
char buffer[ _MAX_PATH ];
741+
::GetModuleFileName( NULL, buffer, sizeof( buffer ) );
742+
if (char *pEnd = strrchr(buffer, '\\'))
743743
{
744744
*(pEnd + 1) = 0;
745745
}
746746

747-
char curbuf[ _MAX_PATH ];
748-
749-
strcpy(curbuf, dirbuf);
750747
SidesInfo *pSide = TheSidesList->getSideInfo(m_curSide);
751748
Dict *d = TheSidesList->getSideInfo(m_curSide)->getDict();
752749
AsciiString name = d->getAsciiString(TheKey_playerName);
753-
strlcat(curbuf, name.str(), ARRAY_SIZE(curbuf));
754-
strlcat(curbuf, "_BuildList", ARRAY_SIZE(curbuf));
755-
strlcat(curbuf, ".ini", ARRAY_SIZE(curbuf));
750+
strlcat(buffer, name.str(), ARRAY_SIZE(buffer));
751+
strlcat(buffer, "_BuildList.ini", ARRAY_SIZE(buffer));
756752

757-
theLogFile = fopen(curbuf, "w");
753+
theLogFile = fopen(buffer, "w");
758754
if (theLogFile == NULL)
759755
throw;
760756

0 commit comments

Comments
 (0)