Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions stl/inc/ostream
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,10 @@ public:
_TRY_BEGIN
_Meta = _Traits::eq_int_type(_Traits::eof(), _Meta) ? _Strbuf->sgetc() : _Strbuf->snextc();
_CATCH_ALL
_Myios::setstate(ios_base::failbit);
_RERAISE;
// N4971 [ostream.inserters]/9: "If an exception was thrown
// while extracting a character, the function sets failbit in the error state,
// and if failbit is set in exceptions() the caught exception is rethrown."
_Myios::setstate(ios_base::failbit, _Myios::exceptions() == ios_base::failbit);
_CATCH_END

if (_Traits::eq_int_type(_Traits::eof(), _Meta)) {
Expand Down
32 changes: 32 additions & 0 deletions tests/std/tests/GH_001858_iostream_exception/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct test_exception {};
template <class CharT>
class throwing_buffer : public basic_streambuf<CharT> {
public:
using typename basic_streambuf<CharT>::int_type;

streampos seekoff(streamoff, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out) override {
throw test_exception{};
}
Expand All @@ -48,6 +50,10 @@ class throwing_buffer : public basic_streambuf<CharT> {
throw test_exception{};
}

int_type underflow() override {
throw test_exception{};
}

basic_streambuf<CharT>* to_buf() {
return this;
}
Expand Down Expand Up @@ -229,6 +235,32 @@ void test_ostream_exceptions() {
// Expected case
}
}

{ // operator<< (testing GH-4322 "<ostream>: Exception from streambuf should be caught and not rethrown")
basic_ostream<CharT> os(buffer.to_buf());

try {
os << &buffer;
} catch (const ios_base::failure&) {
assert(false);
} catch (const test_exception&) {
assert(false);
}
}

{ // operator<< rethrows the caught exception if failbit is set in exceptions()
basic_ostream<CharT> os(buffer.to_buf());
os.exceptions(ios_base::failbit);

try {
os << &buffer;
assert(false);
} catch (const ios_base::failure&) {
assert(false);
} catch (const test_exception&) {
// Expected case
}
}
}

// Also test strengthened and mandatory exception specifications.
Expand Down