Skip to content

Commit

Permalink
Fix writing to stdout when redirected to NUL on Windows (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Dec 30, 2020
1 parent cdc5ef6 commit 9ec5592
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2768,12 +2768,13 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
if (_isatty(fd)) {
detail::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size()));
auto written = detail::dword();
if (!detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
u16.c_str(), static_cast<uint32_t>(u16.size()),
&written, nullptr)) {
FMT_THROW(format_error("failed to write to console"));
if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
u16.c_str(), static_cast<uint32_t>(u16.size()),
&written, nullptr)) {
return;
}
return;
// Fallback to fwrite on failure. It can happen if the output has been
// redirected to NUL.
}
#endif
detail::fwrite_fully(buffer.data(), 1, buffer.size(), f);
Expand Down

0 comments on commit 9ec5592

Please sign in to comment.