Skip to content

Commit 792a4d2

Browse files
committed
xrCore/xrDebug.h|cpp: you should use xr_sprintf, not sprintf
1 parent dc31802 commit 792a4d2

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/xrCore/xrDebug.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -360,44 +360,44 @@ void xrDebug::LogStackTrace(const char* header)
360360
#endif // defined(WINDOWS)
361361

362362

363-
void xrDebug::GatherInfo(char* assertionInfo, const ErrorLocation& loc, const char* expr, const char* desc,
364-
const char* arg1, const char* arg2)
363+
void xrDebug::GatherInfo(char* assertionInfo, size_t bufferSize, const ErrorLocation& loc, const char* expr,
364+
const char* desc, const char* arg1, const char* arg2)
365365
{
366366
char* buffer = assertionInfo;
367367
if (!expr)
368368
expr = "<no expression>";
369369
bool extendedDesc = desc && strchr(desc, '\n');
370370
pcstr prefix = "[error] ";
371-
buffer += sprintf(buffer, "\nFATAL ERROR\n\n");
372-
buffer += sprintf(buffer, "%sExpression : %s\n", prefix, expr);
373-
buffer += sprintf(buffer, "%sFunction : %s\n", prefix, loc.Function);
374-
buffer += sprintf(buffer, "%sFile : %s\n", prefix, loc.File);
375-
buffer += sprintf(buffer, "%sLine : %d\n", prefix, loc.Line);
371+
buffer += xr_sprintf(buffer, bufferSize, "\nFATAL ERROR\n\n");
372+
buffer += xr_sprintf(buffer, bufferSize, "%sExpression : %s\n", prefix, expr);
373+
buffer += xr_sprintf(buffer, bufferSize, "%sFunction : %s\n", prefix, loc.Function);
374+
buffer += xr_sprintf(buffer, bufferSize, "%sFile : %s\n", prefix, loc.File);
375+
buffer += xr_sprintf(buffer, bufferSize, "%sLine : %d\n", prefix, loc.Line);
376376
if (extendedDesc)
377377
{
378-
buffer += sprintf(buffer, "\n%s\n", desc);
378+
buffer += xr_sprintf(buffer, bufferSize, "\n%s\n", desc);
379379
if (arg1)
380380
{
381-
buffer += sprintf(buffer, "%s\n", arg1);
381+
buffer += xr_sprintf(buffer, bufferSize, "%s\n", arg1);
382382
if (arg2)
383-
buffer += sprintf(buffer, "%s\n", arg2);
383+
buffer += xr_sprintf(buffer, bufferSize, "%s\n", arg2);
384384
}
385385
}
386386
else
387387
{
388-
buffer += sprintf(buffer, "%sDescription : %s\n", prefix, desc);
388+
buffer += xr_sprintf(buffer, bufferSize, "%sDescription : %s\n", prefix, desc);
389389
if (arg1)
390390
{
391391
if (arg2)
392392
{
393-
buffer += sprintf(buffer, "%sArgument 0 : %s\n", prefix, arg1);
394-
buffer += sprintf(buffer, "%sArgument 1 : %s\n", prefix, arg2);
393+
buffer += xr_sprintf(buffer, bufferSize, "%sArgument 0 : %s\n", prefix, arg1);
394+
buffer += xr_sprintf(buffer, bufferSize, "%sArgument 1 : %s\n", prefix, arg2);
395395
}
396396
else
397-
buffer += sprintf(buffer, "%sArguments : %s\n", prefix, arg1);
397+
buffer += xr_sprintf(buffer, bufferSize, "%sArguments : %s\n", prefix, arg1);
398398
}
399399
}
400-
buffer += sprintf(buffer, "\n");
400+
buffer += xr_sprintf(buffer, bufferSize, "\n");
401401
if (shared_str_initialized)
402402
{
403403
Log(assertionInfo);
@@ -411,15 +411,15 @@ void xrDebug::GatherInfo(char* assertionInfo, const ErrorLocation& loc, const ch
411411
if (shared_str_initialized)
412412
Log("stack trace:\n");
413413
#ifdef USE_OWN_ERROR_MESSAGE_WINDOW
414-
buffer += sprintf(buffer, "stack trace:\n\n");
414+
buffer += xr_sprintf(buffer, bufferSize, "stack trace:\n\n");
415415
#endif // USE_OWN_ERROR_MESSAGE_WINDOW
416416
xr_vector<xr_string> stackTrace = BuildStackTrace();
417417
for (size_t i = 2; i < stackTrace.size(); i++)
418418
{
419419
if (shared_str_initialized)
420420
Log(stackTrace[i].c_str());
421421
#ifdef USE_OWN_ERROR_MESSAGE_WINDOW
422-
buffer += sprintf(buffer, "%s\n", stackTrace[i].c_str());
422+
buffer += xr_sprintf(buffer, bufferSize, "%s\n", stackTrace[i].c_str());
423423
#endif // USE_OWN_ERROR_MESSAGE_WINDOW
424424
}
425425
if (shared_str_initialized)
@@ -455,7 +455,8 @@ void xrDebug::Fail(bool& ignoreAlways, const ErrorLocation& loc, const char* exp
455455
lock.Enter();
456456
ErrorAfterDialog = true;
457457
string4096 assertionInfo;
458-
GatherInfo(assertionInfo, loc, expr, desc, arg1, arg2);
458+
auto size = sizeof(assertionInfo);
459+
GatherInfo(assertionInfo, sizeof(assertionInfo), loc, expr, desc, arg1, arg2);
459460
#ifdef USE_OWN_ERROR_MESSAGE_WINDOW
460461
xr_strcat(assertionInfo,
461462
"\r\n"
@@ -647,7 +648,7 @@ void xrDebug::SaveMiniDump(EXCEPTION_POINTERS *exPtrs)
647648
string64 dateStr;
648649
timestamp(dateStr);
649650
string_path dumpPath;
650-
sprintf(dumpPath, "%s_%s_%s.mdmp", Core.ApplicationName, Core.UserName, dateStr);
651+
xr_sprintf(dumpPath, sizeof(dumpPath), "%s_%s_%s.mdmp", Core.ApplicationName, Core.UserName, dateStr);
651652
__try
652653
{
653654
if (FS.path_exist("$logs$"))
@@ -657,7 +658,7 @@ void xrDebug::SaveMiniDump(EXCEPTION_POINTERS *exPtrs)
657658
{
658659
string_path temp;
659660
xr_strcpy(temp, dumpPath);
660-
sprintf(dumpPath, "logs/%s", temp);
661+
xr_sprintf(dumpPath, sizeof(dumpPath), "logs/%s", temp);
661662
}
662663
WriteMiniDump(MINIDUMP_TYPE(MiniDumpFilterMemory | MiniDumpScanMemory), dumpPath, GetCurrentThreadId(), exPtrs);
663664
#endif
@@ -677,7 +678,7 @@ void xrDebug::FormatLastError(char* buffer, const size_t& bufferSize)
677678
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, lastErr,
678679
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, nullptr);
679680
// XXX nitrocaster: check buffer overflow
680-
sprintf(buffer, "[error][%8d]: %s", lastErr, (char*)msg);
681+
xr_sprintf(buffer, bufferSize, "[error][%8d]: %s", lastErr, (char*)msg);
681682
LocalFree(msg);
682683
#endif
683684
}
@@ -701,7 +702,7 @@ LONG WINAPI xrDebug::UnhandledFilter(EXCEPTION_POINTERS* exPtrs)
701702
{
702703
if (shared_str_initialized)
703704
Log(stackTrace[i].c_str());
704-
sprintf(buffer, "%s\r\n", stackTrace[i].c_str());
705+
xr_sprintf(buffer, sizeof(buffer), "%s\r\n", stackTrace[i].c_str());
705706
#ifdef DEBUG
706707
if (!IsDebuggerPresent())
707708
os_clipboard::update_clipboard(buffer);

src/xrCore/xrDebug.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class XRCORE_API xrDebug
7171
static void SetDialogHandler(DialogHandler handler) { OnDialog = handler; }
7272
static const char* ErrorToString(long code);
7373
static void SetBugReportFile(const char* fileName);
74-
static void GatherInfo(char* assertionInfo, const ErrorLocation& loc, const char* expr, const char* desc,
75-
const char* arg1 = nullptr, const char* arg2 = nullptr);
74+
static void GatherInfo(char* assertionInfo, size_t bufferSize, const ErrorLocation& loc, const char* expr,
75+
const char* desc, const char* arg1 = nullptr, const char* arg2 = nullptr);
7676
static void Fatal(const ErrorLocation& loc, const char* format, ...);
7777
static void Fail(bool& ignoreAlways, const ErrorLocation& loc, const char* expr, long hresult,
7878
const char* arg1 = nullptr, const char* arg2 = nullptr);

src/xrScriptEngine/DebugMacros.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ do
1111
if (!(expr)) \
1212
{ \
1313
string4096 assertionInfo; \
14-
xrDebug::GatherInfo(assertionInfo, DEBUG_INFO, #expr, nullptr, nullptr, nullptr); \
14+
xrDebug::GatherInfo(assertionInfo, sizeof(assertionInfo), DEBUG_INFO, #expr, nullptr, nullptr, nullptr); \
1515
throw assertionInfo; \
1616
} \
1717
\
@@ -24,7 +24,7 @@ do
2424
if (!(expr)) \
2525
{ \
2626
string4096 assertionInfo; \
27-
xrDebug::GatherInfo(assertionInfo, DEBUG_INFO, #expr, msg0, nullptr, nullptr); \
27+
xrDebug::GatherInfo(assertionInfo, sizeof(assertionInfo), DEBUG_INFO, #expr, msg0, nullptr, nullptr); \
2828
throw assertionInfo; \
2929
} \
3030
\
@@ -37,7 +37,7 @@ do
3737
if (!(expr)) \
3838
{ \
3939
string4096 assertionInfo; \
40-
xrDebug::GatherInfo(assertionInfo, DEBUG_INFO, #expr, msg0, msg1, nullptr); \
40+
xrDebug::GatherInfo(assertionInfo, sizeof(assertionInfo), DEBUG_INFO, #expr, msg0, msg1, nullptr); \
4141
throw assertionInfo; \
4242
} \
4343
\

0 commit comments

Comments
 (0)