Switch to ANSI Windows API in `Win32ErrorString()` function
What changed, and why it matters
This commit changes how Bitcoin Core turns Windows system error numbers into human-readable text. It switches from the modern Unicode ('wide character') version of a Windows API to the older ANSI ('A') version. The change removes C++ standard-library helpers for converting between Unicode and UTF-8. The practical effect is that error messages on Windows may no longer display correctly for languages that need non-ASCII characters; it is not a direct remote-exploitable vulnerability.
Treat as a low-severity code-quality/localization issue. If the project wants fully correct internationalized error messages on Windows, consider keeping a UTF-16 path and converting to UTF-8 via a non-deprecated method (e.g., MultiByteToWideChar/WideCharToMultiByte or a small UTF-16-to-UTF-8 helper). Review whether any downstream logging or RPC output assumes these strings are UTF-8.
Security signals we found
Switch from Unicode to ANSI Windows API
Removal of UTF-16/UTF-8 conversion logic
Potential character-encoding degradation in diagnostic error strings
No bounds-checking changes; buffer size unchanged
Evidence from the diff
The patch replaces FormatMessageW with FormatMessageA and removes std::wstring_convert/std::codecvt_utf8_utf16 conversion. The buffer becomes char[256] instead of wchar_t[256], and the returned string is now the raw ANSI message. This avoids deprecated C++17 codecvt machinery and simplifies the code, but it means Windows error strings are returned in the system’s default ANSI code page rather than UTF-8. On non-English Windows installs this can produce mojibake or lossy character conversion for error messages. There is no evidence in the diff of a buffer overflow, memory corruption, or injection path.
Changed components
src/util/syserror.cppWin32ErrorString()Windows-only error-message formattingInspect captured patch +5 / −10
diff --git a/src/util/syserror.cpp b/src/util/syserror.cpp
index a902826f..4b456db8 100644
--- a/src/util/syserror.cpp
+++ b/src/util/syserror.cpp
@@ -12,8 +12,6 @@
#if defined(WIN32)
#include <windows.h>
-#include <locale>
-#include <codecvt>
#endif
std::string SysErrorString(int err)
@@ -41,16 +39,13 @@ std::string SysErrorString(int err)
#if defined(WIN32)
std::string Win32ErrorString(int err)
{
- wchar_t buf[256];
+ char buf[256];
buf[0] = 0;
- if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
+ if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- buf, ARRAYSIZE(buf), nullptr))
- {
- return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
- }
- else
- {
+ buf, ARRAYSIZE(buf), nullptr)) {
+ return strprintf("%s (%d)", buf, err);
+ } else {
return strprintf("Unknown error (%d)", err);
}
}
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.