aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2025-01-15 13:52:01 +0000
committerJonathan Wakely <redi@gcc.gnu.org>2025-02-07 14:53:30 +0000
commit6e758f378a62747c96feb0ed752af7eae5b92b8f (patch)
tree5bbd6f3e81d6936dc560aa785b6fb2abe794d381 /gcc
parent89f007c2a623c2fd57bf1085435d8a71a9b21029 (diff)
downloadgcc-6e758f378a62747c96feb0ed752af7eae5b92b8f.zip
gcc-6e758f378a62747c96feb0ed752af7eae5b92b8f.tar.gz
gcc-6e758f378a62747c96feb0ed752af7eae5b92b8f.tar.bz2
libstdc++: Handle exceptions in std::ostream::sentry destructor
Because basic_ostream::sentry::~sentry is implicitly noexcept, we can't let any exceptions escape from it, or the program would terminate. If the streambuf's sync() function throws, or if it returns an error and setting badbit in the stream state throws, then the program would terminate. LWG 835 intended to prevent exceptions from being thrown by the std::basic_ostream::sentry destructor, but failed to cover the case where the streambuf's sync() member throws an exception. LWG 4188 is needed to fix that part. In any case, LWG 835 was never implemented for libstdc++ so this does that, as well as my proposed fix for 4188 (that badbit should be set if pubsync() exits via an exception). In order to avoid a second try-catch block to handle an exception that might be thrown by setting badbit, this introduces an RAII helper class that temporarily clears the stream's exceptions mask, then restores it afterwards. The try-catch block doesn't handle the forced_unwind exception explicitly, because catching and rethrowing that would just terminate when it reached the sentry's implicit noexcept(true) anyway. libstdc++-v3/ChangeLog: * include/bits/ostream.h (basic_ostream::_Disable_exceptions): RAII helper type. (basic_ostream::sentry::~sentry): Use _Disable_exceptions. Add try-catch block around call to pubsync. * testsuite/27_io/basic_ostream/exceptions/char/lwg4188.cc: New test. * testsuite/27_io/basic_ostream/exceptions/wchar_t/lwg4188.cc: New test.
Diffstat (limited to 'gcc')
0 files changed, 0 insertions, 0 deletions