From c85e74357356fd0d9b5b0c7711cf514ef47feeea Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 12 May 2024 15:20:39 +1000 Subject: [PATCH] Error: Strip trailing whitespace from Windows errors --- src/common/error.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/common/error.cpp b/src/common/error.cpp index 53da9b3d1..d2ec4db22 100644 --- a/src/common/error.cpp +++ b/src/common/error.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include "fmt/format.h" @@ -99,8 +100,11 @@ void Error::SetWin32(std::string_view prefix, unsigned long err) m_type = Type::Win32; WCHAR buf[128]; - const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf, - static_cast(std::size(buf)), nullptr); + DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf, + static_cast(std::size(buf)), nullptr); + while (r > 0 && std::iswspace(buf[r - 1])) + r--; + if (r > 0) { m_description = @@ -134,8 +138,11 @@ void Error::SetHResult(std::string_view prefix, long err) m_type = Type::HResult; WCHAR buf[128]; - const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf, - static_cast(std::size(buf)), nullptr); + DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf, + static_cast(std::size(buf)), nullptr); + while (r > 0 && std::iswspace(buf[r - 1])) + r--; + if (r > 0) { m_description = fmt::format("{}HRESULT {:08X}: {}", prefix, static_cast(err),