Skip to content

Commit 2126ecd

Browse files
author
Bart Roossien
committed
fix(core): Replace strcat with strlcat
1 parent dbdcd89 commit 2126ecd

File tree

23 files changed

+98
-80
lines changed

23 files changed

+98
-80
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
140140
m_data->peek()[usableNumChars] = 0;
141141
}
142142
if (strToCat)
143-
strcat(m_data->peek(), strToCat);
143+
strlcat(m_data->peek(), strToCat, usableNumChars + 1);
144144
return;
145145
}
146146

@@ -169,7 +169,7 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
169169
newData->peek()[usableNumChars] = 0;
170170
}
171171
if (strToCat)
172-
strcat(newData->peek(), strToCat);
172+
strlcat(newData->peek(), strToCat, usableNumChars + 1);
173173

174174
releaseBuffer();
175175
m_data = newData;

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ static const char *getCurrentTickString(void)
210210
Empty the buffer passed in, then optionally prepend the current TickCount
211211
value in string form, depending on the setting of theDebugFlags.
212212
*/
213-
static void prepBuffer(char *buffer)
213+
static void prepBuffer(char *buffer, int size)
214214
{
215215
buffer[0] = 0;
216216
#ifdef ALLOW_DEBUG_UTILS
217217
if (theDebugFlags & DEBUG_FLAG_PREPEND_TIME)
218218
{
219219
strcpy(buffer, getCurrentTickString());
220-
strcat(buffer, " ");
220+
strlcat(buffer, " ", size);
221221
}
222222
#endif
223223
}
@@ -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);
@@ -436,7 +436,7 @@ void DebugLog(const char *format, ...)
436436
if (theDebugFlags == 0)
437437
MessageBoxWrapper("DebugLog - Debug not inited properly", "", MB_OK|MB_TASKMODAL);
438438

439-
prepBuffer(theBuffer);
439+
prepBuffer(theBuffer, ARRAY_SIZE(theBuffer));
440440

441441
va_list args;
442442
va_start(args, format);
@@ -508,8 +508,8 @@ void DebugCrash(const char *format, ...)
508508
// make it big to avoid weird overflow bugs in debug mode
509509
char theCrashBuffer[ LARGE_BUFFER ];
510510

511-
prepBuffer(theCrashBuffer);
512-
strcat(theCrashBuffer, "ASSERTION FAILURE: ");
511+
prepBuffer(theCrashBuffer, LARGE_BUFFER);
512+
strlcat(theCrashBuffer, "ASSERTION FAILURE: ", LARGE_BUFFER);
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", LARGE_BUFFER);
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, _MAX_PATH);
741741
strcpy(curbuf, TheGlobalData->getPath_UserData().str());
742-
strcat(curbuf, RELEASECRASH_FILE_NAME);
742+
strlcat(curbuf, RELEASECRASH_FILE_NAME, _MAX_PATH);
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, _MAX_PATH);
830830
strcpy(curbuf, TheGlobalData->getPath_UserData().str());
831-
strcat(curbuf, RELEASECRASH_FILE_NAME);
831+
strlcat(curbuf, RELEASECRASH_FILE_NAME, _MAX_PATH);
832832

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

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,9 +2974,10 @@ void MemoryPoolFactory::memoryPoolUsageReport( const char* filename, FILE *appen
29742974

29752975
if( !appendToFileInstead )
29762976
{
2977-
char tmp[256];
2977+
const int tmpSize = 256;
2978+
char tmp[tmpSize];
29782979
strcpy(tmp,filename);
2979-
strcat(tmp,".csv");
2980+
strlcat(tmp,".csv", tmpSize);
29802981
perfStatsFile = fopen(tmp, "w");
29812982
}
29822983
else
@@ -3201,12 +3202,13 @@ void MemoryPoolFactory::debugMemoryReport(Int flags, Int startCheckpoint, Int en
32013202
DEBUG_LOG(("------------------------------------------"));
32023203
DEBUG_LOG(("Begin Block Report for %s", nm));
32033204
DEBUG_LOG(("------------------------------------------"));
3204-
char buf[256] = "";
3205-
if (flags & _REPORT_CP_ALLOCATED_BEFORE) strcat(buf, "AllocBefore ");
3206-
if (flags & _REPORT_CP_ALLOCATED_BETWEEN) strcat(buf, "AllocBetween ");
3207-
if (flags & _REPORT_CP_FREED_BEFORE) strcat(buf, "FreedBefore ");
3208-
if (flags & _REPORT_CP_FREED_BETWEEN) strcat(buf, "FreedBetween ");
3209-
if (flags & _REPORT_CP_FREED_NEVER) strcat(buf, "StillExisting ");
3205+
const int bufSize = 256;
3206+
char buf[bufSize] = "";
3207+
if (flags & _REPORT_CP_ALLOCATED_BEFORE) strlcat(buf, "AllocBefore ", bufSize);
3208+
if (flags & _REPORT_CP_ALLOCATED_BETWEEN) strlcat(buf, "AllocBetween ", bufSize);
3209+
if (flags & _REPORT_CP_FREED_BEFORE) strlcat(buf, "FreedBefore ", bufSize);
3210+
if (flags & _REPORT_CP_FREED_BETWEEN) strlcat(buf, "FreedBetween ", bufSize);
3211+
if (flags & _REPORT_CP_FREED_NEVER) strlcat(buf, "StillExisting ", bufSize);
32103212
DEBUG_LOG(("Options: Between checkpoints %d and %d, report on (%s)",startCheckpoint,endCheckpoint,buf));
32113213
DEBUG_LOG(("------------------------------------------"));
32123214

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", _MAX_PATH);
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
@@ -236,8 +236,8 @@ bool HModelDefClass::read_connection(ChunkLoadClass & cload,HmdlNodeDefStruct *
236236
}
237237

238238
strcpy(node->RenderObjName,ModelName);
239-
strcat(node->RenderObjName,".");
240-
strcat(node->RenderObjName,con.RenderObjName);
239+
strlcat(node->RenderObjName,".", ARRAY_SIZE(node->RenderObjName));
240+
strlcat(node->RenderObjName, con.RenderObjName, ARRAY_SIZE(node->RenderObjName));
241241

242242
if (pre30) {
243243
if (con.PivotIdx == 65535) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ static const char * Make_W3D_Filename (const char *w3d_name)
558558

559559
// Copy the w3d name into a static buffer, turn it into lowercase
560560
// letters, and append a ".w3d" file extension. That's the filename.
561-
static char buffer[64];
561+
static const int bufferSize = 64;
562+
static char buffer[bufferSize];
562563
if (*w3d_name == 0)
563564
{
564565
// Empty W3D name case.
@@ -570,6 +571,6 @@ static const char * Make_W3D_Filename (const char *w3d_name)
570571
if (dot)
571572
*dot = 0;
572573
strlwr(buffer);
573-
strcat(buffer, ".w3d");
574+
strlcat(buffer, ".w3d", bufferSize);
574575
return buffer;
575576
}

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

Lines changed: 6 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 <stringex.h>
3940
//#include "wlib/wstring.h"
4041

4142
#include "DownloadDebug.h"
@@ -678,7 +679,7 @@ HRESULT Cftp::FindFile( LPCSTR szRemoteFileName, int * piSize )
678679
_splitpath( szRemoteFileName, NULL, m_szRemoteFilePath+strlen(m_szRemoteFilePath),
679680
m_szRemoteFileName, ext );
680681

681-
strcat( m_szRemoteFileName, ext );
682+
strlcat(m_szRemoteFileName, ext, sizeof(m_szRemoteFileName));
682683

683684
for( i = 0; i < (int)strlen( m_szRemoteFilePath ); i++ )
684685
{
@@ -1708,7 +1709,8 @@ HRESULT Cftp::FileRecoveryPosition( LPCSTR szLocalFileName, LPCSTR szRegistryRo
17081709
FILE * testfp;
17091710
HKEY hkey;
17101711
unsigned char regfilename[ 256 ];
1711-
char regkey[ 512 ];
1712+
const int regkeySize = 512;
1713+
char regkey[ regkeySize ];
17121714
unsigned long t1, t2;
17131715
17141716
if( ( szRegistryRoot == NULL ) || ( szLocalFileName == NULL ) )
@@ -1722,11 +1724,11 @@ HRESULT Cftp::FileRecoveryPosition( LPCSTR szLocalFileName, LPCSTR szRegistryRo
17221724
strcpy( regkey, szRegistryRoot );
17231725
if( regkey[ strlen( regkey ) - 1 ] != '\\' )
17241726
{
1725-
strcat( regkey, "\\Download" );
1727+
strlcat( regkey, "\\Download", regkeySize);
17261728
}
17271729
else
17281730
{
1729-
strcat( regkey, "Download" );
1731+
strlcat( regkey, "Download", regkeySize);
17301732
}
17311733
17321734
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, (LPCTSTR)regkey,

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ char const * Last_Error_Text(void)
224224
*=============================================================================================*/
225225
static void Add_Txt (char const *txt)
226226
{
227-
if (strlen(ExceptionText) + strlen(txt) < 65535) {
228-
strcat(ExceptionText, txt);
227+
if (strlen(ExceptionText) + strlen(txt) < ARRAY_SIZE(ExceptionText) - 1) {
228+
strlcat(ExceptionText, txt, ARRAY_SIZE(ExceptionText));
229229
}
230230
#if (0)
231231
/*
@@ -340,7 +340,8 @@ void Dump_Exception_Info(EXCEPTION_POINTERS *e_info)
340340
/*
341341
** Scrap buffer for constructing dump strings
342342
*/
343-
char scrap [256];
343+
const int scrapSize = 256;
344+
char scrap [scrapSize];
344345

345346
/*
346347
** Clear out the dump buffer
@@ -653,15 +654,15 @@ void Dump_Exception_Info(EXCEPTION_POINTERS *e_info)
653654

654655
for (int c = 0 ; c < 32 ; c++) {
655656
if (IsBadReadPtr(eip_ptr, 1)) {
656-
strcat(scrap, "?? ");
657+
strlcat(scrap, "?? ", scrapSize);
657658
} else {
658659
sprintf(bytestr, "%02X ", *eip_ptr);
659-
strcat(scrap, bytestr);
660+
strlcat(scrap, bytestr, scrapSize);
660661
}
661662
eip_ptr++;
662663
}
663664

664-
strcat(scrap, "\r\n\r\n");
665+
strlcat(scrap, "\r\n\r\n", scrapSize);
665666
Add_Txt(scrap);
666667

667668
/*
@@ -677,14 +678,14 @@ void Dump_Exception_Info(EXCEPTION_POINTERS *e_info)
677678
** The stack contents cannot be read so just print up question marks.
678679
*/
679680
sprintf(scrap, "%p: ", static_cast<void*>(stackptr));
680-
strcat(scrap, "????????\r\n");
681+
strlcat(scrap, "????????\r\n", scrapSize);
681682
} else {
682683
/*
683684
** If this stack address is in our memory space then try to match it with a code symbol.
684685
*/
685686
if (IsBadCodePtr((FARPROC)*stackptr)) {
686687
sprintf(scrap, "%p: %08lX ", static_cast<void*>(stackptr), *stackptr);
687-
strcat(scrap, "DATA_PTR\r\n");
688+
strlcat(scrap, "DATA_PTR\r\n", scrapSize);
688689
} else {
689690
sprintf(scrap, "%p: %08lX", static_cast<void*>(stackptr), *stackptr);
690691

@@ -697,12 +698,12 @@ void Dump_Exception_Info(EXCEPTION_POINTERS *e_info)
697698
if (_SymGetSymFromAddr != NULL && _SymGetSymFromAddr (GetCurrentProcess(), *stackptr, &displacement, symptr)) {
698699
char symbuf[256];
699700
sprintf(symbuf, " - %s + %08X", symptr->Name, displacement);
700-
strcat(scrap, symbuf);
701+
strlcat(scrap, symbuf, scrapSize);
701702
}
702703
} else {
703-
strcat (scrap, " *");
704+
strlcat(scrap, " *", scrapSize);
704705
}
705-
strcat (scrap, "\r\n");
706+
strlcat(scrap, "\r\n", scrapSize);
706707
}
707708
}
708709
Add_Txt(scrap);

0 commit comments

Comments
 (0)