From 291b0075bb8563859d7c0507d36b64b839417bf3 Mon Sep 17 00:00:00 2001 From: Junliang HU Date: Mon, 29 Mar 2021 21:00:31 +0800 Subject: [PATCH 1/2] Add a test case: move ostream while holding data --- test/os-test.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/os-test.cc b/test/os-test.cc index 44a2988fb9c5..d9d094ced476 100644 --- a/test/os-test.cc +++ b/test/os-test.cc @@ -293,6 +293,19 @@ TEST(OStreamTest, Move) { moved.print("hello"); } +TEST(OStreamTest, MoveWhileHoldingData) { + { + fmt::ostream out = fmt::output_file("test-file"); + out.print("Hello, "); + fmt::ostream moved(std::move(out)); + moved.print("world!\n"); + } + { + file in("test-file", file::RDONLY); + EXPECT_READ(in, "Hello, world!\n"); + } +} + TEST(OStreamTest, Print) { fmt::ostream out = fmt::output_file("test-file"); out.print("The answer is {}.\n", fmt::join(std::initializer_list{42}, ", ")); From b77d2a8c85339d296b91d6916742ab25300c5542 Mon Sep 17 00:00:00 2001 From: Junliang HU Date: Mon, 29 Mar 2021 21:01:07 +0800 Subject: [PATCH 2/2] Fix moving ostream while holding data --- include/fmt/os.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/fmt/os.h b/include/fmt/os.h index 0b0b94ed48d8..78971e378604 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -396,8 +396,10 @@ class ostream final : private detail::buffer { ostream(ostream&& other) : detail::buffer(other.data(), other.size(), other.capacity()), file_(std::move(other.file_)) { + other.clear(); other.set(nullptr, 0); } + ~ostream() { flush(); delete[] data();