Skip to content

Commit

Permalink
xrCore/xrDebug.h|cpp: you should use xr_sprintf, not sprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Jul 24, 2018
1 parent 86dd005 commit ebba48b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
45 changes: 23 additions & 22 deletions src/xrCore/xrDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<no expression>";
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);
Expand All @@ -411,15 +411,15 @@ 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<xr_string> stackTrace = BuildStackTrace();
for (size_t i = 2; i < stackTrace.size(); i++)
{
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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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$"))
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/xrCore/xrDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/xrScriptEngine/DebugMacros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
} \
\
Expand All @@ -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; \
} \
\
Expand All @@ -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; \
} \
\
Expand Down

0 comments on commit ebba48b

Please sign in to comment.