Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/source/Core/Utilities/Log/muConsoleDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ CmuConsoleDebug::~CmuConsoleDebug()

CmuConsoleDebug* CmuConsoleDebug::GetInstance()
{
#ifdef CSK_LH_DEBUG_CONSOLE
// Always return a valid instance. Previously returned nullptr in builds
// without CSK_LH_DEBUG_CONSOLE, which made every g_ConsoleDebug->Write
// call site a null-deref in disguise (it "worked" only because the Write
// body was empty when CONSOLE_DEBUG was undefined). Returning a real
// instance is required for the always-on MCD_ERROR path below to be safe.
static CmuConsoleDebug sInstance;
return &sInstance;
#else
return 0;
#endif
}

void CmuConsoleDebug::UpdateMainScene()
Expand Down Expand Up @@ -232,6 +233,18 @@ bool CmuConsoleDebug::CheckCommand(const std::wstring& strCommand)

void CmuConsoleDebug::Write(int iType, const wchar_t* pStr, ...)
{
// MCD_ERROR is always logged to MuError.log, regardless of CONSOLE_DEBUG.
// Other log levels remain debug-only so they don't spam production logs.
if (iType == MCD_ERROR)
{
wchar_t szErrorBuffer[256] = L"";
va_list pArgsForFile;
va_start(pArgsForFile, pStr);
vswprintf(szErrorBuffer, pStr, pArgsForFile);
va_end(pArgsForFile);
Comment on lines +240 to +244
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The buffer szErrorBuffer is limited to 256 characters, which may be insufficient for detailed error messages (e.g., long file paths or complex packet diagnostics). More importantly, the use of vswprintf with only 3 arguments is non-standard and unsafe as it lacks a buffer size limit, risking a buffer overflow if the formatted string exceeds the buffer capacity. Note that CErrorReport::Write in ErrorReport.cpp correctly uses the 4-argument version of vswprintf with a 1024-character limit. To ensure safety and consistency, the buffer size should be increased and the size-limited version of the formatting function should be used.

Suggested change
wchar_t szErrorBuffer[256] = L"";
va_list pArgsForFile;
va_start(pArgsForFile, pStr);
vswprintf(szErrorBuffer, pStr, pArgsForFile);
va_end(pArgsForFile);
wchar_t szErrorBuffer[1024] = L"";
va_list pArgsForFile;
va_start(pArgsForFile, pStr);
vswprintf(szErrorBuffer, 1024, pStr, pArgsForFile);
va_end(pArgsForFile);

g_ErrorReport.Write(L"[MCD_ERROR] %ls\r\n", szErrorBuffer);
}

#ifdef CONSOLE_DEBUG
if (m_bInit)
{
Expand Down
Loading