From 72991fbe9a63fa5215ce498cdcdce737de78e59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 28 Oct 2022 00:21:26 +0200 Subject: [PATCH] Use dynamic buffer size in `CLoggerWindowsDebugger` Make sure the Windows debugger logger does not fail when log messages are too long. The first call to `MultiByteToWideChar` gets the required size of the wide-char buffer, which should be at least one for the null termination. The second call is expected to produce exactly the same number of characters. --- src/base/log.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/base/log.cpp b/src/base/log.cpp index dbe46911a..1cd0ce30a 100644 --- a/src/base/log.cpp +++ b/src/base/log.cpp @@ -423,9 +423,12 @@ class CLoggerWindowsDebugger : public ILogger public: void Log(const CLogMessage *pMessage) override { - WCHAR aWBuffer[4096]; - MultiByteToWideChar(CP_UTF8, 0, pMessage->m_aLine, -1, aWBuffer, sizeof(aWBuffer) / sizeof(WCHAR)); - OutputDebugStringW(aWBuffer); + int WLen = MultiByteToWideChar(CP_UTF8, 0, pMessage->m_aLine, -1, NULL, 0); + dbg_assert(WLen > 0, "MultiByteToWideChar failure"); + WCHAR *pWide = (WCHAR *)malloc(WLen * sizeof(*pWide)); + dbg_assert(MultiByteToWideChar(CP_UTF8, 0, pMessage->m_aLine, -1, pWide, WLen) == WLen, "MultiByteToWideChar failure"); + OutputDebugStringW(pWide); + free(pWide); } }; std::unique_ptr log_logger_windows_debugger()