diff --git a/src/os.cc b/src/os.cc index 4840a658..f0e71aac 100644 --- a/src/os.cc +++ b/src/os.cc @@ -383,7 +383,9 @@ void ostream::grow(buffer& buf, size_t) { ostream::ostream(cstring_view path, const detail::ostream_params& params) : buffer(grow), file_(path, params.oflag) { - set(new char[params.buffer_size], params.buffer_size); + // grow() can only make room by flushing, so the buffer can't be empty. + size_t size = max_of(params.buffer_size, 1); + set(new char[size], size); } ostream::ostream(ostream&& other) noexcept diff --git a/test/os-test.cc b/test/os-test.cc index 5bc9f7eb..24f96ed9 100644 --- a/test/os-test.cc +++ b/test/os-test.cc @@ -295,6 +295,15 @@ TEST(ostream_test, buffer_size) { EXPECT_READ(in, "foo"); } +TEST(ostream_test, zero_buffer_size) { + auto test_file = uniq_file_name(__LINE__); + fmt::ostream out = fmt::output_file(test_file, fmt::buffer_size = 0); + out.print("{}{}", 'f', "oo"); + out.close(); + file in(test_file, file::RDONLY); + EXPECT_READ(in, "foo"); +} + TEST(ostream_test, truncate) { auto test_file = uniq_file_name(__LINE__); {