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 521568d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 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
23 changes: 22 additions & 1 deletion 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 @@ -1847,6 +1849,25 @@ TEST(format_test, line_buffering) {
}
#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] = {};
fread(buf, 1, sizeof(buf), read_end.get());
fgets(buf, sizeof(buf), read_end.get());
EXPECT_STREQ(buf, "1234 567");
}

#endif // FMT_USE_FCNTL

struct deadlockable {
int value = 0;
mutable std::mutex mutex;
Expand Down

0 comments on commit 521568d

Please sign in to comment.