From db4309fc867e3c2198947e31331321e1bd9129d4 Mon Sep 17 00:00:00 2001 From: Mosch0512 Date: Fri, 22 May 2026 17:09:17 +0200 Subject: [PATCH] MCD_ERROR: log to MuError.log unconditionally; fix GetInstance UB Two coupled fixes that make MCD_ERROR diagnostics actually visible in Release builds: 1. CmuConsoleDebug::GetInstance() now always returns a valid static instance. The previous behaviour (return nullptr unless CSK_LH_DEBUG_CONSOLE was defined) made every g_ConsoleDebug->Write call site a null-pointer member-function call. It "worked" only because the Write body was empty without CONSOLE_DEBUG; any code added there would have crashed in Release. 2. Write() now routes MCD_ERROR to g_ErrorReport.Write (file-based, always compiled) before the existing CONSOLE_DEBUG-gated path. MCD_SEND / MCD_RECEIVE / MCD_NORMAL stay debug-only so production logs don't get flooded. Net effect: any MCD_ERROR call site -- including the safe_cast size mismatch diagnostics added in PR #66 -- now lands in MuError.log even in Release builds. --- .../Core/Utilities/Log/muConsoleDebug.cpp | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/source/Core/Utilities/Log/muConsoleDebug.cpp b/src/source/Core/Utilities/Log/muConsoleDebug.cpp index 92655c9b2..b2bd2cf40 100644 --- a/src/source/Core/Utilities/Log/muConsoleDebug.cpp +++ b/src/source/Core/Utilities/Log/muConsoleDebug.cpp @@ -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() @@ -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); + g_ErrorReport.Write(L"[MCD_ERROR] %ls\r\n", szErrorBuffer); + } + #ifdef CONSOLE_DEBUG if (m_bInit) {