From 9ec5592bb5344e191731adce8695a51cc365c68f Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 30 Dec 2020 12:20:17 -0800 Subject: [PATCH] Fix writing to stdout when redirected to NUL on Windows (#2080) --- include/fmt/format-inl.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index a362468f..09a1e749 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -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(_get_osfhandle(fd)), - u16.c_str(), static_cast(u16.size()), - &written, nullptr)) { - FMT_THROW(format_error("failed to write to console")); + if (detail::WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), + u16.c_str(), static_cast(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);