Clarify ostream capacity comment and strengthen zero buffer test

The old test only caught the overflow under ASan.
This commit is contained in:
Victor Zverovich 2026-11-07 16:38:44 -08:00
parent a9e41a6df2
commit 1b5222af1b
2 changed files with 5 additions and 4 deletions

View File

@ -383,7 +383,7 @@ void ostream::grow(buffer<char>& buf, size_t) {
ostream::ostream(cstring_view path, const detail::ostream_params& params)
: buffer<char>(grow), file_(path, params.oflag) {
// grow() can only make room by flushing, so the buffer can't be empty.
// Flushing is the only way to make room, so the capacity can't be zero.
size_t size = max_of<size_t>(params.buffer_size, 1);
set(new char[size], size);
}

View File

@ -298,10 +298,11 @@ TEST(ostream_test, buffer_size) {
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();
out.print("{}{}", 'A', " byte late!");
file in(test_file, file::RDONLY);
EXPECT_READ(in, "foo");
EXPECT_READ(in, "A byte late");
out.close();
EXPECT_READ(in, "!");
}
TEST(ostream_test, truncate) {