Skip to content

Commit 5e5c9e6

Browse files
committed
fix(debug): Avoid theoretical memory corruptions in debug functions (#1371)
1 parent 8840cb8 commit 5e5c9e6

File tree

2 files changed

+24
-10
lines changed
  • GeneralsMD/Code/GameEngine/Source/Common/System
  • Generals/Code/GameEngine/Source/Common/System

2 files changed

+24
-10
lines changed

Generals/Code/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static const char *getCurrentTimeString(void)
192192
static const char *getCurrentTickString(void)
193193
{
194194
static char TheTickString[32];
195-
sprintf(TheTickString, "(T=%08lx)",::GetTickCount());
195+
snprintf(TheTickString, ARRAY_SIZE(TheTickString), "(T=%08lx)", ::GetTickCount());
196196
return TheTickString;
197197
}
198198

@@ -385,14 +385,20 @@ void DebugInit(int flags)
385385
strcat(theLogFileNamePrev, gAppPrefix);
386386
strcat(theLogFileNamePrev, DEBUG_FILE_NAME_PREV);
387387
if (rts::ClientInstance::getInstanceId() > 1u)
388-
sprintf(theLogFileNamePrev + strlen(theLogFileNamePrev), "_Instance%.2u", rts::ClientInstance::getInstanceId());
388+
{
389+
size_t offset = strlen(theLogFileNamePrev);
390+
snprintf(theLogFileNamePrev + offset, ARRAY_SIZE(theLogFileNamePrev) - offset, "_Instance%.2u", rts::ClientInstance::getInstanceId());
391+
}
389392
strcat(theLogFileNamePrev, ".txt");
390393

391394
strcpy(theLogFileName, dirbuf);
392395
strcat(theLogFileName, gAppPrefix);
393396
strcat(theLogFileName, DEBUG_FILE_NAME);
394397
if (rts::ClientInstance::getInstanceId() > 1u)
395-
sprintf(theLogFileName + strlen(theLogFileName), "_Instance%.2u", rts::ClientInstance::getInstanceId());
398+
{
399+
size_t offset = strlen(theLogFileNamePrev);
400+
snprintf(theLogFileName + offset, ARRAY_SIZE(theLogFileName) - offset, "_Instance%.2u", rts::ClientInstance::getInstanceId());
401+
}
396402
strcat(theLogFileName, ".txt");
397403

398404
remove(theLogFileNamePrev);
@@ -428,7 +434,8 @@ void DebugLog(const char *format, ...)
428434

429435
va_list args;
430436
va_start(args, format);
431-
vsprintf(theBuffer + strlen(theBuffer), format, args);
437+
size_t offset = strlen(theBuffer);
438+
vsnprintf(theBuffer + offset, ARRAY_SIZE(theBuffer) - offset, format, args);
432439
va_end(args);
433440

434441
if (strlen(theBuffer) >= sizeof(theBuffer))
@@ -454,7 +461,7 @@ void DebugLogRaw(const char *format, ...)
454461

455462
va_list args;
456463
va_start(args, format);
457-
vsprintf(theBuffer, format, args);
464+
vsnprintf(theBuffer, ARRAY_SIZE(theBuffer), format, args);
458465
va_end(args);
459466

460467
if (strlen(theBuffer) >= sizeof(theBuffer))

GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static const char *getCurrentTimeString(void)
193193
static const char *getCurrentTickString(void)
194194
{
195195
static char TheTickString[32];
196-
sprintf(TheTickString, "(T=%08lx)",::GetTickCount());
196+
snprintf(TheTickString, ARRAY_SIZE(TheTickString), "(T=%08lx)", ::GetTickCount());
197197
return TheTickString;
198198
}
199199

@@ -386,14 +386,20 @@ void DebugInit(int flags)
386386
strcat(theLogFileNamePrev, gAppPrefix);
387387
strcat(theLogFileNamePrev, DEBUG_FILE_NAME_PREV);
388388
if (rts::ClientInstance::getInstanceId() > 1u)
389-
sprintf(theLogFileNamePrev + strlen(theLogFileNamePrev), "_Instance%.2u", rts::ClientInstance::getInstanceId());
389+
{
390+
size_t offset = strlen(theLogFileNamePrev);
391+
snprintf(theLogFileNamePrev + offset, ARRAY_SIZE(theLogFileNamePrev) - offset, "_Instance%.2u", rts::ClientInstance::getInstanceId());
392+
}
390393
strcat(theLogFileNamePrev, ".txt");
391394

392395
strcpy(theLogFileName, dirbuf);
393396
strcat(theLogFileName, gAppPrefix);
394397
strcat(theLogFileName, DEBUG_FILE_NAME);
395398
if (rts::ClientInstance::getInstanceId() > 1u)
396-
sprintf(theLogFileName + strlen(theLogFileName), "_Instance%.2u", rts::ClientInstance::getInstanceId());
399+
{
400+
size_t offset = strlen(theLogFileNamePrev);
401+
snprintf(theLogFileName + offset, ARRAY_SIZE(theLogFileName) - offset, "_Instance%.2u", rts::ClientInstance::getInstanceId());
402+
}
397403
strcat(theLogFileName, ".txt");
398404

399405
remove(theLogFileNamePrev);
@@ -429,7 +435,8 @@ void DebugLog(const char *format, ...)
429435

430436
va_list args;
431437
va_start(args, format);
432-
vsprintf(theBuffer + strlen(theBuffer), format, args);
438+
size_t offset = strlen(theBuffer);
439+
vsnprintf(theBuffer + offset, ARRAY_SIZE(theBuffer) - offset, format, args);
433440
va_end(args);
434441

435442
if (strlen(theBuffer) >= sizeof(theBuffer))
@@ -455,7 +462,7 @@ void DebugLogRaw(const char *format, ...)
455462

456463
va_list args;
457464
va_start(args, format);
458-
vsprintf(theBuffer, format, args);
465+
vsnprintf(theBuffer, ARRAY_SIZE(theBuffer), format, args);
459466
va_end(args);
460467

461468
if (strlen(theBuffer) >= sizeof(theBuffer))

0 commit comments

Comments
 (0)