Corrently handle buffer flush

This commit is contained in:
Victor Zverovich 2024-11-09 09:48:26 -08:00
parent 5a3576acc8
commit a35389b3c2
2 changed files with 24 additions and 3 deletions

View File

@ -505,8 +505,8 @@ constexpr auto to_pointer(OutputIt, size_t) -> T* {
template <typename T> template <typename T>
FMT_CONSTEXPR20 auto to_pointer(basic_appender<T> it, size_t n) -> T* { FMT_CONSTEXPR20 auto to_pointer(basic_appender<T> it, size_t n) -> T* {
buffer<T>& buf = get_container(it); buffer<T>& buf = get_container(it);
buf.try_reserve(buf.size() + n);
auto size = buf.size(); auto size = buf.size();
buf.try_reserve(size + n);
if (buf.capacity() < size + n) return nullptr; if (buf.capacity() < size + n) return nullptr;
buf.try_resize(size + n); buf.try_resize(size + n);
return buf.data() + size; return buf.data() + size;

View File

@ -1821,7 +1821,9 @@ TEST(format_test, big_print) {
} }
// Windows CRT implements _IOLBF incorrectly (full buffering). // Windows CRT implements _IOLBF incorrectly (full buffering).
#if FMT_USE_FCNTL && !defined(_WIN32) #if FMT_USE_FCNTL
# ifndef _WIN32
TEST(format_test, line_buffering) { TEST(format_test, line_buffering) {
auto pipe = fmt::pipe(); auto pipe = fmt::pipe();
@ -1845,7 +1847,26 @@ TEST(format_test, line_buffering) {
reader.join(); 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] = {};
size_t n = fread(buf, 1, sizeof(buf), read_end.get());
EXPECT_EQ(n, sizeof(buf));
EXPECT_STREQ(fgets(buf, sizeof(buf), read_end.get()), "1234 567");
}
#endif // FMT_USE_FCNTL
struct deadlockable { struct deadlockable {
int value = 0; int value = 0;