Commit 615c1eef authored by Victor Zverovich's avatar Victor Zverovich

Fix error handling in fmt::fprintf.

parent 8ea9f068
......@@ -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<int>(size);
}
// Explicit instantiations for char.
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment