Fix non-ASCII messages garbled in logging::SystemErrorCodeToString
Utilize FormatMessageW to retrieve the error message, treating it as UTF-16 encoding. Convert the obtained message to UTF-8 encoding and return it, ensuring proper preservation of non-ASCII characters. Bug: 1515262 Change-Id: I06b015047c14142eb8547c0f1349ee755db9a56e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5166531 Commit-Queue: Bruce Dawson <brucedawson@chromium.org> Reviewed-by: Bruce Dawson <brucedawson@chromium.org> Reviewed-by: Peter Boström <pbos@chromium.org> Cr-Commit-Position: refs/heads/main@{#1243440}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
890b011110
commit
8b7f180c0c
1
AUTHORS
1
AUTHORS
@ -1470,6 +1470,7 @@ Yael Aharon <yael.aharon@intel.com>
|
||||
Yan Wang <yan0422.wang@samsung.com>
|
||||
Yang Gu <yang.gu@intel.com>
|
||||
Yang Liu <jd9668954@gmail.com>
|
||||
Yang Liu <yangliu.leo@bytedance.com>
|
||||
Yannay Hammer <yannayha@gmail.com>
|
||||
Yannic Bonenberger <yannic.bonenberger@gmail.com>
|
||||
Yarin Kaul <yarin.kaul@gmail.com>
|
||||
|
@ -1047,14 +1047,17 @@ SystemErrorCode GetLastSystemErrorCode() {
|
||||
|
||||
BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
const int kErrorMessageBufferSize = 256;
|
||||
char msgbuf[kErrorMessageBufferSize];
|
||||
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
|
||||
DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf,
|
||||
std::size(msgbuf), nullptr);
|
||||
LPWSTR msgbuf = nullptr;
|
||||
DWORD len = ::FormatMessageW(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr, error_code, 0, reinterpret_cast<LPWSTR>(&msgbuf), 0, nullptr);
|
||||
if (len) {
|
||||
std::u16string message = base::WideToUTF16(msgbuf);
|
||||
::LocalFree(msgbuf);
|
||||
msgbuf = nullptr;
|
||||
// Messages returned by system end with line breaks.
|
||||
return base::CollapseWhitespaceASCII(msgbuf, true) +
|
||||
return base::UTF16ToUTF8(base::CollapseWhitespace(message, true)) +
|
||||
base::StringPrintf(" (0x%lX)", error_code);
|
||||
}
|
||||
return base::StringPrintf("Error (0x%lX) while retrieving error. (0x%lX)",
|
||||
|
Reference in New Issue
Block a user