Skip to content

Commit

Permalink
Fix exception propagation from iterators (#2097)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 24, 2021
1 parent acef0bb commit ce519e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,9 @@ void buffer<T>::append(const U* begin, const U* end) {

template <typename OutputIt, typename T, typename Traits>
void iterator_buffer<OutputIt, T, Traits>::flush() {
out_ = copy_str<T>(data_, data_ + this->limit(this->size()), out_);
auto size = this->size();
this->clear();
out_ = copy_str<T>(data_, data_ + this->limit(size), out_);
}
} // namespace detail

Expand Down
11 changes: 11 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit ce519e9

Please sign in to comment.