From 75da7a522e40789a53d176f8e3d3f1631e415aad Mon Sep 17 00:00:00 2001 From: Cristian Morales Vega Date: Tue, 25 Feb 2020 11:37:06 +0000 Subject: [PATCH] Fix ostream flush: fix #1853, close #1859, close #1866 After calling the DynamicBuffer commit() function the buffer returned from prepare() can be invalidated but the ostream kept using it. --- CHANGELOG.md | 1 + include/boost/beast/core/detail/ostream.hpp | 2 ++ test/beast/core/ostream.cpp | 25 +++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49434e8a..0890c13a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Version 286: * Fix ostream warning * Field digest is endian-independent * update broken links in README +* Fix ostream flush API Changes: diff --git a/include/boost/beast/core/detail/ostream.hpp b/include/boost/beast/core/detail/ostream.hpp index 05861d30..e7646beb 100644 --- a/include/boost/beast/core/detail/ostream.hpp +++ b/include/boost/beast/core/detail/ostream.hpp @@ -100,6 +100,7 @@ public: b_.commit( (this->pptr() - this->pbase()) * sizeof(CharT)); + this->setp(nullptr, nullptr); return 0; } }; @@ -169,6 +170,7 @@ public: b_.commit( (this->pptr() - this->pbase()) * sizeof(CharT)); + this->setp(nullptr, nullptr); return 0; } }; diff --git a/test/beast/core/ostream.cpp b/test/beast/core/ostream.cpp index 180199f3..a1319b88 100644 --- a/test/beast/core/ostream.cpp +++ b/test/beast/core/ostream.cpp @@ -66,6 +66,31 @@ public: fail("wrong exception", __FILE__, __LINE__); } } + + // flush + { + // Issue #1853 + flat_static_buffer<16> b; + auto half_view = string_view(s.data(), 8); + { + auto os = ostream(b); + os << half_view; + os.flush(); + } + BEAST_EXPECT(buffers_to_string(b.data()) == half_view); + } + + { + flat_static_buffer<16> b; + { + auto os = ostream(b); + os << string_view(s.data(), 8); + os.flush(); + os << string_view(s.data() + 8, 8); + os.flush(); + } + BEAST_EXPECT(buffers_to_string(b.data()) == s); + } } void