Fix error handling in fmt::fprintf.

This commit is contained in:
Victor Zverovich 2014-11-14 09:40:01 -08:00
parent 8ea9f068c7
commit 615c1eef6b
2 changed files with 9 additions and 1 deletions

View File

@ -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.

View File

@ -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