diff --git a/src/xrCore/xrDebug.cpp b/src/xrCore/xrDebug.cpp index 7ad036ef047..6072170f868 100644 --- a/src/xrCore/xrDebug.cpp +++ b/src/xrCore/xrDebug.cpp @@ -360,44 +360,44 @@ void xrDebug::LogStackTrace(const char* header) #endif // defined(WINDOWS) -void xrDebug::GatherInfo(char* assertionInfo, const ErrorLocation& loc, const char* expr, const char* desc, - const char* arg1, const char* arg2) +void xrDebug::GatherInfo(char* assertionInfo, size_t bufferSize, const ErrorLocation& loc, const char* expr, + const char* desc, const char* arg1, const char* arg2) { char* buffer = assertionInfo; if (!expr) expr = ""; bool extendedDesc = desc && strchr(desc, '\n'); pcstr prefix = "[error] "; - buffer += sprintf(buffer, "\nFATAL ERROR\n\n"); - buffer += sprintf(buffer, "%sExpression : %s\n", prefix, expr); - buffer += sprintf(buffer, "%sFunction : %s\n", prefix, loc.Function); - buffer += sprintf(buffer, "%sFile : %s\n", prefix, loc.File); - buffer += sprintf(buffer, "%sLine : %d\n", prefix, loc.Line); + buffer += xr_sprintf(buffer, bufferSize, "\nFATAL ERROR\n\n"); + buffer += xr_sprintf(buffer, bufferSize, "%sExpression : %s\n", prefix, expr); + buffer += xr_sprintf(buffer, bufferSize, "%sFunction : %s\n", prefix, loc.Function); + buffer += xr_sprintf(buffer, bufferSize, "%sFile : %s\n", prefix, loc.File); + buffer += xr_sprintf(buffer, bufferSize, "%sLine : %d\n", prefix, loc.Line); if (extendedDesc) { - buffer += sprintf(buffer, "\n%s\n", desc); + buffer += xr_sprintf(buffer, bufferSize, "\n%s\n", desc); if (arg1) { - buffer += sprintf(buffer, "%s\n", arg1); + buffer += xr_sprintf(buffer, bufferSize, "%s\n", arg1); if (arg2) - buffer += sprintf(buffer, "%s\n", arg2); + buffer += xr_sprintf(buffer, bufferSize, "%s\n", arg2); } } else { - buffer += sprintf(buffer, "%sDescription : %s\n", prefix, desc); + buffer += xr_sprintf(buffer, bufferSize, "%sDescription : %s\n", prefix, desc); if (arg1) { if (arg2) { - buffer += sprintf(buffer, "%sArgument 0 : %s\n", prefix, arg1); - buffer += sprintf(buffer, "%sArgument 1 : %s\n", prefix, arg2); + buffer += xr_sprintf(buffer, bufferSize, "%sArgument 0 : %s\n", prefix, arg1); + buffer += xr_sprintf(buffer, bufferSize, "%sArgument 1 : %s\n", prefix, arg2); } else - buffer += sprintf(buffer, "%sArguments : %s\n", prefix, arg1); + buffer += xr_sprintf(buffer, bufferSize, "%sArguments : %s\n", prefix, arg1); } } - buffer += sprintf(buffer, "\n"); + buffer += xr_sprintf(buffer, bufferSize, "\n"); if (shared_str_initialized) { Log(assertionInfo); @@ -411,7 +411,7 @@ void xrDebug::GatherInfo(char* assertionInfo, const ErrorLocation& loc, const ch if (shared_str_initialized) Log("stack trace:\n"); #ifdef USE_OWN_ERROR_MESSAGE_WINDOW - buffer += sprintf(buffer, "stack trace:\n\n"); + buffer += xr_sprintf(buffer, bufferSize, "stack trace:\n\n"); #endif // USE_OWN_ERROR_MESSAGE_WINDOW xr_vector stackTrace = BuildStackTrace(); for (size_t i = 2; i < stackTrace.size(); i++) @@ -419,7 +419,7 @@ void xrDebug::GatherInfo(char* assertionInfo, const ErrorLocation& loc, const ch if (shared_str_initialized) Log(stackTrace[i].c_str()); #ifdef USE_OWN_ERROR_MESSAGE_WINDOW - buffer += sprintf(buffer, "%s\n", stackTrace[i].c_str()); + buffer += xr_sprintf(buffer, bufferSize, "%s\n", stackTrace[i].c_str()); #endif // USE_OWN_ERROR_MESSAGE_WINDOW } if (shared_str_initialized) @@ -455,7 +455,8 @@ void xrDebug::Fail(bool& ignoreAlways, const ErrorLocation& loc, const char* exp lock.Enter(); ErrorAfterDialog = true; string4096 assertionInfo; - GatherInfo(assertionInfo, loc, expr, desc, arg1, arg2); + auto size = sizeof(assertionInfo); + GatherInfo(assertionInfo, sizeof(assertionInfo), loc, expr, desc, arg1, arg2); #ifdef USE_OWN_ERROR_MESSAGE_WINDOW xr_strcat(assertionInfo, "\r\n" @@ -647,7 +648,7 @@ void xrDebug::SaveMiniDump(EXCEPTION_POINTERS *exPtrs) string64 dateStr; timestamp(dateStr); string_path dumpPath; - sprintf(dumpPath, "%s_%s_%s.mdmp", Core.ApplicationName, Core.UserName, dateStr); + xr_sprintf(dumpPath, sizeof(dumpPath), "%s_%s_%s.mdmp", Core.ApplicationName, Core.UserName, dateStr); __try { if (FS.path_exist("$logs$")) @@ -657,7 +658,7 @@ void xrDebug::SaveMiniDump(EXCEPTION_POINTERS *exPtrs) { string_path temp; xr_strcpy(temp, dumpPath); - sprintf(dumpPath, "logs/%s", temp); + xr_sprintf(dumpPath, sizeof(dumpPath), "logs/%s", temp); } WriteMiniDump(MINIDUMP_TYPE(MiniDumpFilterMemory | MiniDumpScanMemory), dumpPath, GetCurrentThreadId(), exPtrs); #endif @@ -677,7 +678,7 @@ void xrDebug::FormatLastError(char* buffer, const size_t& bufferSize) FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, lastErr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, nullptr); // XXX nitrocaster: check buffer overflow - sprintf(buffer, "[error][%8d]: %s", lastErr, (char*)msg); + xr_sprintf(buffer, bufferSize, "[error][%8d]: %s", lastErr, (char*)msg); LocalFree(msg); #endif } @@ -701,7 +702,7 @@ LONG WINAPI xrDebug::UnhandledFilter(EXCEPTION_POINTERS* exPtrs) { if (shared_str_initialized) Log(stackTrace[i].c_str()); - sprintf(buffer, "%s\r\n", stackTrace[i].c_str()); + xr_sprintf(buffer, sizeof(buffer), "%s\r\n", stackTrace[i].c_str()); #ifdef DEBUG if (!IsDebuggerPresent()) os_clipboard::update_clipboard(buffer); diff --git a/src/xrCore/xrDebug.h b/src/xrCore/xrDebug.h index c3650bbc2b0..754305cab6f 100644 --- a/src/xrCore/xrDebug.h +++ b/src/xrCore/xrDebug.h @@ -71,8 +71,8 @@ class XRCORE_API xrDebug static void SetDialogHandler(DialogHandler handler) { OnDialog = handler; } static const char* ErrorToString(long code); static void SetBugReportFile(const char* fileName); - static void GatherInfo(char* assertionInfo, const ErrorLocation& loc, const char* expr, const char* desc, - const char* arg1 = nullptr, const char* arg2 = nullptr); + static void GatherInfo(char* assertionInfo, size_t bufferSize, const ErrorLocation& loc, const char* expr, + const char* desc, const char* arg1 = nullptr, const char* arg2 = nullptr); static void Fatal(const ErrorLocation& loc, const char* format, ...); static void Fail(bool& ignoreAlways, const ErrorLocation& loc, const char* expr, long hresult, const char* arg1 = nullptr, const char* arg2 = nullptr); diff --git a/src/xrScriptEngine/DebugMacros.hpp b/src/xrScriptEngine/DebugMacros.hpp index 68aab2a6135..94e971058aa 100644 --- a/src/xrScriptEngine/DebugMacros.hpp +++ b/src/xrScriptEngine/DebugMacros.hpp @@ -11,7 +11,7 @@ do if (!(expr)) \ { \ string4096 assertionInfo; \ - xrDebug::GatherInfo(assertionInfo, DEBUG_INFO, #expr, nullptr, nullptr, nullptr); \ + xrDebug::GatherInfo(assertionInfo, sizeof(assertionInfo), DEBUG_INFO, #expr, nullptr, nullptr, nullptr); \ throw assertionInfo; \ } \ \ @@ -24,7 +24,7 @@ do if (!(expr)) \ { \ string4096 assertionInfo; \ - xrDebug::GatherInfo(assertionInfo, DEBUG_INFO, #expr, msg0, nullptr, nullptr); \ + xrDebug::GatherInfo(assertionInfo, sizeof(assertionInfo), DEBUG_INFO, #expr, msg0, nullptr, nullptr); \ throw assertionInfo; \ } \ \ @@ -37,7 +37,7 @@ do if (!(expr)) \ { \ string4096 assertionInfo; \ - xrDebug::GatherInfo(assertionInfo, DEBUG_INFO, #expr, msg0, msg1, nullptr); \ + xrDebug::GatherInfo(assertionInfo, sizeof(assertionInfo), DEBUG_INFO, #expr, msg0, msg1, nullptr); \ throw assertionInfo; \ } \ \