Skip to content

Commit

Permalink
Corrently handle buffer flush
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 9, 2024
1 parent 5a3576a commit 0d8f28d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ constexpr auto to_pointer(OutputIt, size_t) -> T* {
template <typename T>
FMT_CONSTEXPR20 auto to_pointer(basic_appender<T> it, size_t n) -> T* {
buffer<T>& buf = get_container(it);
buf.try_reserve(buf.size() + n);
auto size = buf.size();
buf.try_reserve(size + n);
if (buf.capacity() < size + n) return nullptr;
buf.try_resize(size + n);
return buf.data() + size;
Expand Down
25 changes: 23 additions & 2 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,9 @@ TEST(format_test, big_print) {
}

// Windows CRT implements _IOLBF incorrectly (full buffering).
#if FMT_USE_FCNTL && !defined(_WIN32)
#if FMT_USE_FCNTL

# ifndef _WIN32
TEST(format_test, line_buffering) {
auto pipe = fmt::pipe();

Expand All @@ -1845,7 +1847,26 @@ TEST(format_test, line_buffering) {

reader.join();
}
#endif
# endif

TEST(format_test, buffer_boundary) {
auto pipe = fmt::pipe();

auto write_end = pipe.write_end.fdopen("w");
setvbuf(write_end.get(), nullptr, _IOFBF, 4096);
for (int i = 3; i < 4094; i++)
write_end.print("{}", (i % 73) != 0 ? 'x' : '\n');
write_end.print("{} {}", 1234, 567);
write_end.close();

auto read_end = pipe.read_end.fdopen("r");
char buf[4091] = {};
(void)fread(buf, 1, sizeof(buf), read_end.get());
(void)fgets(buf, sizeof(buf), read_end.get());
EXPECT_STREQ(buf, "1234 567");
}

#endif // FMT_USE_FCNTL

struct deadlockable {
int value = 0;
Expand Down

0 comments on commit 0d8f28d

Please sign in to comment.