diff --git a/include/fmt/format.h b/include/fmt/format.h index 52e2e7c4..df533081 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -658,8 +658,9 @@ void buffer::append(const U* begin, const U* end) { template void iterator_buffer::flush() { - out_ = copy_str(data_, data_ + this->limit(this->size()), out_); + auto size = this->size(); this->clear(); + out_ = copy_str(data_, data_ + this->limit(size), out_); } } // namespace detail diff --git a/test/format-test.cc b/test/format-test.cc index 899a8b4b..37c22e1a 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2021,6 +2021,17 @@ TEST(FormatTest, FormatTo) { EXPECT_EQ(string_view(v.data(), v.size()), "foo"); } +struct nongrowing_container { + using value_type = char; + void push_back(char) { throw std::runtime_error("can't take it any more"); } +}; + +TEST(FormatTest, FormatToPropagatesExceptions) { + auto c = nongrowing_container(); + EXPECT_THROW(fmt::format_to(std::back_inserter(c), "{}", 42), + std::runtime_error); +} + TEST(FormatTest, FormatToN) { char buffer[4]; buffer[3] = 'x';