Skip to content

Commit ba08667

Browse files
committed
lib/fmt/SystemError: remove unnecessary FormatMessageA() suffix
This causes the Windows error message to occur twice. And it causes libfmt to crash if CP_ACP is not ASCII-compatible (e.g. codepage-932 / shift_jis) due to our use of FormatMessageA(). See also MusicPlayerDaemon/MPD#2305 for a discussion. Closes MusicPlayerDaemon/MPD#2302
1 parent 3e43d79 commit ba08667

1 file changed

Lines changed: 2 additions & 20 deletions

File tree

src/lib/fmt/SystemError.cxx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,12 @@ VFmtSystemError(std::error_code code,
1414

1515
#ifdef _WIN32
1616

17-
#include <array>
18-
19-
#include <windef.h> // for HWND (needed by winbase.h)
20-
#include <winbase.h> // for FormatMessageA()
21-
2217
std::system_error
2318
VFmtLastError(DWORD code,
2419
fmt::string_view format_str, fmt::format_args args) noexcept
2520
{
26-
std::array<char, 512> buffer;
27-
const auto end = buffer.data() + buffer.size();
28-
29-
constexpr std::size_t max_prefix = sizeof(buffer) - 128;
30-
auto [p, _] = fmt::vformat_to_n(buffer.data(),
31-
buffer.size() - max_prefix,
32-
format_str, args);
33-
*p++ = ':';
34-
*p++ = ' ';
35-
36-
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
37-
FORMAT_MESSAGE_IGNORE_INSERTS,
38-
nullptr, code, 0, p, end - p, nullptr);
39-
40-
return MakeLastError(code, buffer.data());
21+
const auto msg = VFmtBuffer<512>(format_str, args);
22+
return MakeLastError(code, msg);
4123
}
4224

4325
#endif // _WIN32

0 commit comments

Comments
 (0)