Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rethrowing the caught exception from streambuf #4372

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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]/8-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
33 changes: 33 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,33 @@ void test_ostream_exceptions() {
// Expected case
}
}

{ // operator<< with exceptions
basic_ostream<CharT> os(buffer.to_buf());
os.exceptions(ios_base::goodbit);

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

{ // operator<< rethrow 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