From 615c1eef6b5068b8c0920676bb2fd5309527f3d9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 14 Nov 2014 09:40:01 -0800 Subject: [PATCH] Fix error handling in fmt::fprintf. --- format.cc | 3 ++- test/printf-test.cc | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/format.cc b/format.cc index a2c892a5..82019f89 100644 --- a/format.cc +++ b/format.cc @@ -1087,7 +1087,8 @@ void fmt::print_colored(Color c, StringRef format, ArgList args) { int fmt::fprintf(std::FILE *f, StringRef format, ArgList args) { MemoryWriter w; printf(w, format, args); - return std::fwrite(w.data(), 1, w.size(), f); + std::size_t size = w.size(); + return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast(size); } // Explicit instantiations for char. diff --git a/test/printf-test.cc b/test/printf-test.cc index 8e65e00a..eeae701c 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -438,4 +438,11 @@ TEST(PrintfTest, Examples) { EXPECT_WRITE(stdout, fmt::printf("%1$s, %3$d %2$s", weekday, month, day), "Thursday, 21 August"); } + +TEST(PrintfTest, PrintfError) { + fmt::File read_end, write_end; + fmt::File::pipe(read_end, write_end); + int result = fmt::fprintf(read_end.fdopen("r").get(), "test"); + EXPECT_LT(result, 0); +} #endif