Skip to content

Commit 47b4cdc

Browse files
authored
fix: Replace strcat with strlcat for robustness (#1685)
1 parent 14211bb commit 47b4cdc

File tree

67 files changed

+222
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+222
-215
lines changed

Core/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,24 +388,24 @@ void DebugInit(int flags)
388388
}
389389

390390
strcpy(theLogFileNamePrev, dirbuf);
391-
strcat(theLogFileNamePrev, gAppPrefix);
392-
strcat(theLogFileNamePrev, DEBUG_FILE_NAME_PREV);
391+
strlcat(theLogFileNamePrev, gAppPrefix, ARRAY_SIZE(theLogFileNamePrev));
392+
strlcat(theLogFileNamePrev, DEBUG_FILE_NAME_PREV, ARRAY_SIZE(theLogFileNamePrev));
393393
if (rts::ClientInstance::getInstanceId() > 1u)
394394
{
395395
size_t offset = strlen(theLogFileNamePrev);
396396
snprintf(theLogFileNamePrev + offset, ARRAY_SIZE(theLogFileNamePrev) - offset, "_Instance%.2u", rts::ClientInstance::getInstanceId());
397397
}
398-
strcat(theLogFileNamePrev, ".txt");
398+
strlcat(theLogFileNamePrev, ".txt", ARRAY_SIZE(theLogFileNamePrev));
399399

400400
strcpy(theLogFileName, dirbuf);
401-
strcat(theLogFileName, gAppPrefix);
402-
strcat(theLogFileName, DEBUG_FILE_NAME);
401+
strlcat(theLogFileName, gAppPrefix, ARRAY_SIZE(theLogFileNamePrev));
402+
strlcat(theLogFileName, DEBUG_FILE_NAME, ARRAY_SIZE(theLogFileNamePrev));
403403
if (rts::ClientInstance::getInstanceId() > 1u)
404404
{
405405
size_t offset = strlen(theLogFileName);
406406
snprintf(theLogFileName + offset, ARRAY_SIZE(theLogFileName) - offset, "_Instance%.2u", rts::ClientInstance::getInstanceId());
407407
}
408-
strcat(theLogFileName, ".txt");
408+
strlcat(theLogFileName, ".txt", ARRAY_SIZE(theLogFileNamePrev));
409409

410410
remove(theLogFileNamePrev);
411411
rename(theLogFileName, theLogFileNamePrev);
@@ -509,7 +509,7 @@ void DebugCrash(const char *format, ...)
509509
char theCrashBuffer[ LARGE_BUFFER ];
510510

511511
prepBuffer(theCrashBuffer);
512-
strcat(theCrashBuffer, "ASSERTION FAILURE: ");
512+
strlcat(theCrashBuffer, "ASSERTION FAILURE: ", ARRAY_SIZE(theCrashBuffer));
513513

514514
va_list arg;
515515
va_start(arg, format);
@@ -538,7 +538,7 @@ void DebugCrash(const char *format, ...)
538538
#endif
539539
}
540540

541-
strcat(theCrashBuffer, "\n\nAbort->exception; Retry->debugger; Ignore->continue");
541+
strlcat(theCrashBuffer, "\n\nAbort->exception; Retry->debugger; Ignore->continue", ARRAY_SIZE(theCrashBuffer));
542542

543543
const int result = doCrashBox(theCrashBuffer, useLogging);
544544

@@ -737,9 +737,9 @@ void ReleaseCrash(const char *reason)
737737
}
738738

739739
strcpy(prevbuf, TheGlobalData->getPath_UserData().str());
740-
strcat(prevbuf, RELEASECRASH_FILE_NAME_PREV);
740+
strlcat(prevbuf, RELEASECRASH_FILE_NAME_PREV, ARRAY_SIZE(prevbuf));
741741
strcpy(curbuf, TheGlobalData->getPath_UserData().str());
742-
strcat(curbuf, RELEASECRASH_FILE_NAME);
742+
strlcat(curbuf, RELEASECRASH_FILE_NAME, ARRAY_SIZE(curbuf));
743743

744744
remove(prevbuf);
745745
rename(curbuf, prevbuf);
@@ -826,9 +826,9 @@ void ReleaseCrashLocalized(const AsciiString& p, const AsciiString& m)
826826
char curbuf[ _MAX_PATH ];
827827

828828
strcpy(prevbuf, TheGlobalData->getPath_UserData().str());
829-
strcat(prevbuf, RELEASECRASH_FILE_NAME_PREV);
829+
strlcat(prevbuf, RELEASECRASH_FILE_NAME_PREV, ARRAY_SIZE(prevbuf));
830830
strcpy(curbuf, TheGlobalData->getPath_UserData().str());
831-
strcat(curbuf, RELEASECRASH_FILE_NAME);
831+
strlcat(curbuf, RELEASECRASH_FILE_NAME, ARRAY_SIZE(curbuf));
832832

833833
remove(prevbuf);
834834
rename(curbuf, prevbuf);

Core/GameEngine/Source/Common/System/GameMemory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2976,7 +2976,7 @@ void MemoryPoolFactory::memoryPoolUsageReport( const char* filename, FILE *appen
29762976
{
29772977
char tmp[256];
29782978
strcpy(tmp,filename);
2979-
strcat(tmp,".csv");
2979+
strlcat(tmp, ".csv", ARRAY_SIZE(tmp));
29802980
perfStatsFile = fopen(tmp, "w");
29812981
}
29822982
else

Core/GameEngine/Source/Common/System/GameMemoryInit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void userMemoryManagerInitPools()
123123
}
124124
--pEnd;
125125
}
126-
strcat(buf, "\\Data\\INI\\MemoryPools.ini");
126+
strlcat(buf, "\\Data\\INI\\MemoryPools.ini", ARRAY_SIZE(buf));
127127

128128
FILE* fp = fopen(buf, "r");
129129
if (fp)

Core/Libraries/Source/WWVegas/WW3D2/hcanim.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
257257
cload.Close_Chunk();
258258

259259
strcpy(Name,aheader.HierarchyName);
260-
strcat(Name,".");
261-
strcat(Name,aheader.Name);
260+
strlcat(Name, ".", ARRAY_SIZE(Name));
261+
strlcat(Name, aheader.Name, ARRAY_SIZE(Name));
262262

263263
// TSS chasing crash bug 05/26/99
264264
WWASSERT(HierarchyName != NULL);

Core/Libraries/Source/WWVegas/WW3D2/hmdldef.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ bool HModelDefClass::read_connection(ChunkLoadClass & cload,HmdlNodeDefStruct *
234234
}
235235

236236
strcpy(node->RenderObjName,ModelName);
237-
strcat(node->RenderObjName,".");
238-
strcat(node->RenderObjName,con.RenderObjName);
237+
strlcat(node->RenderObjName, ".", ARRAY_SIZE(node->RenderObjName));
238+
strlcat(node->RenderObjName, con.RenderObjName, ARRAY_SIZE(node->RenderObjName));
239239

240240
if (pre30) {
241241
if (con.PivotIdx == 65535) {

Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,6 @@ static const char * Make_W3D_Filename (const char *w3d_name)
570570
if (dot)
571571
*dot = 0;
572572
strlwr(buffer);
573-
strcat(buffer, ".w3d");
573+
strlcat(buffer, ".w3d", ARRAY_SIZE(buffer));
574574
return buffer;
575575
}

Core/Libraries/Source/WWVegas/WWDownload/FTP.CPP

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <time.h>
3737
#include <direct.h>
3838
#include <errno.h>
39+
#include <WWCommon.h>
3940
//#include "wlib/wstring.h"
4041

4142
#include "stringex.h"
@@ -141,7 +142,7 @@ static bool Use_Non_Blocking_Mode(void)
141142
if ((regRetval!=ERROR_SUCCESS) || (type != REG_DWORD))
142143
return(TRUE);
143144

144-
return(bool(value));
145+
return bool(value);
145146
}
146147

147148

@@ -679,7 +680,7 @@ HRESULT Cftp::FindFile( LPCSTR szRemoteFileName, int * piSize )
679680
_splitpath( szRemoteFileName, NULL, m_szRemoteFilePath+strlen(m_szRemoteFilePath),
680681
m_szRemoteFileName, ext );
681682

682-
strcat( m_szRemoteFileName, ext );
683+
strlcat(m_szRemoteFileName, ext, ARRAY_SIZE(m_szRemoteFileName));
683684

684685
for( i = 0; i < (int)strlen( m_szRemoteFilePath ); i++ )
685686
{
@@ -1723,11 +1724,11 @@ HRESULT Cftp::FileRecoveryPosition( LPCSTR szLocalFileName, LPCSTR szRegistryRo
17231724
strcpy( regkey, szRegistryRoot );
17241725
if( regkey[ strlen( regkey ) - 1 ] != '\\' )
17251726
{
1726-
strcat( regkey, "\\Download" );
1727+
strlcat(regkey, "\\Download", ARRAY_SIZE(regkey));
17271728
}
17281729
else
17291730
{
1730-
strcat( regkey, "Download" );
1731+
strlcat(regkey, "Download", ARRAY_SIZE(regkey));
17311732
}
17321733
17331734
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, (LPCTSTR)regkey,

Core/Libraries/Source/WWVegas/WWLib/Except.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ char const * Last_Error_Text(void)
224224
*=============================================================================================*/
225225
static void Add_Txt (char const *txt)
226226
{
227-
if (strlen(ExceptionText) + strlen(txt) < 65535) {
227+
if (strlen(ExceptionText) + strlen(txt) < ARRAY_SIZE(ExceptionText) - 1) {
228228
strcat(ExceptionText, txt);
229229
}
230230
#if (0)
@@ -653,15 +653,15 @@ void Dump_Exception_Info(EXCEPTION_POINTERS *e_info)
653653

654654
for (int c = 0 ; c < 32 ; c++) {
655655
if (IsBadReadPtr(eip_ptr, 1)) {
656-
strcat(scrap, "?? ");
656+
strlcat(scrap, "?? ", ARRAY_SIZE(scrap));
657657
} else {
658658
sprintf(bytestr, "%02X ", *eip_ptr);
659-
strcat(scrap, bytestr);
659+
strlcat(scrap, bytestr, ARRAY_SIZE(scrap));
660660
}
661661
eip_ptr++;
662662
}
663663

664-
strcat(scrap, "\r\n\r\n");
664+
strlcat(scrap, "\r\n\r\n", ARRAY_SIZE(scrap));
665665
Add_Txt(scrap);
666666

667667
/*
@@ -677,14 +677,14 @@ void Dump_Exception_Info(EXCEPTION_POINTERS *e_info)
677677
** The stack contents cannot be read so just print up question marks.
678678
*/
679679
sprintf(scrap, "%p: ", static_cast<void*>(stackptr));
680-
strcat(scrap, "????????\r\n");
680+
strlcat(scrap, "????????\r\n", ARRAY_SIZE(scrap));
681681
} else {
682682
/*
683683
** If this stack address is in our memory space then try to match it with a code symbol.
684684
*/
685685
if (IsBadCodePtr((FARPROC)*stackptr)) {
686686
sprintf(scrap, "%p: %08lX ", static_cast<void*>(stackptr), *stackptr);
687-
strcat(scrap, "DATA_PTR\r\n");
687+
strlcat(scrap, "DATA_PTR\r\n", ARRAY_SIZE(scrap));
688688
} else {
689689
sprintf(scrap, "%p: %08lX", static_cast<void*>(stackptr), *stackptr);
690690

@@ -697,12 +697,12 @@ void Dump_Exception_Info(EXCEPTION_POINTERS *e_info)
697697
if (_SymGetSymFromAddr != NULL && _SymGetSymFromAddr (GetCurrentProcess(), *stackptr, &displacement, symptr)) {
698698
char symbuf[256];
699699
sprintf(symbuf, " - %s + %08X", symptr->Name, displacement);
700-
strcat(scrap, symbuf);
700+
strlcat(scrap, symbuf, ARRAY_SIZE(scrap));
701701
}
702702
} else {
703-
strcat (scrap, " *");
703+
strlcat(scrap, " *", ARRAY_SIZE(scrap));
704704
}
705-
strcat (scrap, "\r\n");
705+
strlcat(scrap, "\r\n", ARRAY_SIZE(scrap));
706706
}
707707
}
708708
Add_Txt(scrap);

Core/Libraries/Source/WWVegas/WWLib/registry.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ void RegistryClass::Save_Registry_Values(HKEY key, char *path, INIClass *ini)
386386
*/
387387
case REG_DWORD:
388388
strcpy(save_name, "DWORD_");
389-
strcat(save_name, value_name);
389+
strlcat(save_name, value_name, ARRAY_SIZE(save_name));
390390
ini->Put_Int(path, save_name, *((unsigned long*)data));
391391
break;
392392

@@ -395,7 +395,7 @@ void RegistryClass::Save_Registry_Values(HKEY key, char *path, INIClass *ini)
395395
*/
396396
case REG_SZ:
397397
strcpy(save_name, "STRING_");
398-
strcat(save_name, value_name);
398+
strlcat(save_name, value_name, ARRAY_SIZE(save_name));
399399
ini->Put_String(path, save_name, (char*)data);
400400
break;
401401

@@ -404,7 +404,7 @@ void RegistryClass::Save_Registry_Values(HKEY key, char *path, INIClass *ini)
404404
*/
405405
case REG_BINARY:
406406
strcpy(save_name, "BIN_");
407-
strcat(save_name, value_name);
407+
strlcat(save_name, value_name, ARRAY_SIZE(save_name));
408408
ini->Put_UUBlock(path, save_name, (char*)data, data_size);
409409
break;
410410

@@ -471,8 +471,8 @@ void RegistryClass::Save_Registry_Tree(char *path, INIClass *ini)
471471
*/
472472
char new_key_path[512];
473473
strcpy(new_key_path, path);
474-
strcat(new_key_path, "\\");
475-
strcat(new_key_path, name);
474+
strlcat(new_key_path, "\\", ARRAY_SIZE(new_key_path));
475+
strlcat(new_key_path, name, ARRAY_SIZE(new_key_path));
476476

477477
unsigned long num_subs = 0;
478478
unsigned long num_values = 0;
@@ -567,7 +567,7 @@ void RegistryClass::Load_Registry(const char *filename, char *old_path, char *ne
567567
strcpy(path, new_path);
568568
char *cut = strstr(section_name, old_path);
569569
if (cut) {
570-
strcat(path, cut + old_path_len);
570+
strlcat(path, cut + old_path_len, ARRAY_SIZE(path));
571571
}
572572

573573
/*
@@ -694,8 +694,8 @@ void RegistryClass::Delete_Registry_Tree(char *path)
694694
*/
695695
char new_key_path[512];
696696
strcpy(new_key_path, path);
697-
strcat(new_key_path, "\\");
698-
strcat(new_key_path, name);
697+
strlcat(new_key_path, "\\", ARRAY_SIZE(new_key_path));
698+
strlcat(new_key_path, name, ARRAY_SIZE(new_key_path));
699699

700700
unsigned long num_subs = 0;
701701
unsigned long num_values = 0;

Core/Libraries/Source/debug/debug_debug.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdlib.h>
3232
#include <Utility/stdio_adapter.h>
3333
#include <string.h>
34+
#include <WWCommon.h>
3435
#include <new> // needed for placement new prototype
3536

3637
// a little dummy variable that makes the linker actually include
@@ -347,7 +348,7 @@ bool Debug::AssertDone(void)
347348
"time being (stops logging this assertion as well).";
348349
char *help=(char *)DebugAllocMemory(ioBuffer[curType].used+strlen(addInfo)+1);
349350
strcpy(help,ioBuffer[curType].buffer+82);
350-
strcat(help,addInfo);
351+
strlcat(help, addInfo, ARRAY_SIZE(help));
351352

352353
// First hit? Then do a stack trace
353354
if (curFrameEntry->hits==1)
@@ -611,7 +612,7 @@ bool Debug::CrashDone(bool die)
611612
#endif
612613
char *help=(char *)DebugAllocMemory(ioBuffer[curType].used+strlen(addInfo)+1);
613614
strcpy(help,ioBuffer[curType].buffer+82);
614-
strcat(help,addInfo);
615+
strlcat(help, addInfo, ARRAY_SIZE(help));
615616

616617
// First hit? Then do a stack trace
617618
if (curFrameEntry->hits==1)

0 commit comments

Comments
 (0)