Title
ostream::sentry destructor should handle exceptions
Status
ready
Section
[ostream.sentry]
Submitter
Jonathan Wakely

Created on 2025-01-14.00:00:00 last changed 3 days ago

Messages

Date: 2025-02-07.22:53:34

Proposed resolution:

This wording is relative to N5001.

  1. Modify [ostream.sentry] as indicated:

    ~sentry();
    -4- If (os.flags() & ios_base::unitbuf) && !uncaught_exceptions() && os.good() is `true`, calls os.rdbuf()->pubsync(). If that function returns −1 or exits via an exception, sets `badbit` in `os.rdstate()` without propagating an exception.
Date: 2025-02-15.00:00:00

[ 2025-02-07; Reflector poll ]

Set status to Tentatively Ready after six votes in favour during reflector poll.

Date: 2025-01-14.00:00:00

LWG 397 suggested changing [ostream.sentry] to say that the `ostream::sentry` destructor doesn't throw any exceptions. That issue was closed as resolved by LWG 835 which included the "Throws: Nothing" change to the `sentry` destructor. However, that part of the resolution never seems to have been applied to the working draft. N3091 mentions applying LWG 835 for N3090 but the destructor change is missing, maybe because the paragraph for the sentry destructor had been renumbered from p17 to p4 and LWG 835 didn't show sufficient context to indicate the intended location.

The problem described in LWG 397 is still present: the streambuf operations can fail, and the sentry needs to handle that. The changes for LWG 835 ensure no exception is thrown if rdbuf()->pubsync() returns -1 on failure, but do nothing for the case where it throws an exception (the original topic of LWG 397!). Because C++11 made `~sentry` implicitly `noexcept`, an exception from `rdbuf()->pubsync()` will terminate the process. That needs to be fixed.

Libstdc++ does terminate if `pubsync()` throws when called by `~sentry`. Both MSVC and Libc++ silently swallow exceptions. It seems preferable to handle the exception and report an error, just as we do when `pubsync()` returns -1.

History
Date User Action Args
2025-02-07 22:53:34adminsetmessages: + msg14622
2025-02-07 22:53:34adminsetstatus: new -> ready
2025-01-14 12:05:06adminsetmessages: + msg14533
2025-01-14 00:00:00admincreate