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

Implement LWG-3571 and LWG-3570: flush_emit set badbit if the emit call fails #2418

Merged
merged 18 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
14 changes: 13 additions & 1 deletion stl/inc/ostream
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,19 @@ basic_ostream<_Elem, _Traits>& flush_emit(basic_ostream<_Elem, _Traits>& _Ostr)
_Ostr.flush();
const auto _Sync_buf_ptr = dynamic_cast<_Basic_syncbuf_impl<_Elem, _Traits>*>(_Ostr.rdbuf());
if (_Sync_buf_ptr) {
_Sync_buf_ptr->_Do_emit();
ios_base::iostate _State = ios_base::goodbit;
const typename basic_ostream<_Elem, _Traits>::sentry _Ok(_Ostr);
if (!_Ok) {
_State |= ios_base::badbit;
} else {
_TRY_IO_BEGIN
const bool _Emit_failed = !_Sync_buf_ptr->_Do_emit();
if (_Emit_failed) {
_State |= ios_base::badbit;
}
_CATCH_IO_(ios_base, _Ostr)
}
_Ostr.setstate(_State);
}
return _Ostr;
}
Expand Down
14 changes: 12 additions & 2 deletions stl/inc/syncstream
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ public:
using syncbuf_type = basic_syncbuf<_Elem, _Traits, _Alloc>;

using _Mybase = basic_ostream<_Elem, _Traits>;
using _Myios = basic_ios<_Elem, _Traits>;

basic_osyncstream(streambuf_type* _Strbuf, const _Alloc& _Al)
: _Mybase(_STD addressof(_Sync_buf)), _Sync_buf(_Strbuf, _Al) {}
Expand All @@ -344,9 +345,18 @@ public:
basic_osyncstream& operator=(basic_osyncstream&&) noexcept = default;

void emit() {
if (!_Sync_buf.emit()) {
_Mybase::setstate(ios::badbit);
ios_base::iostate _State = ios_base::goodbit;
const typename _Mybase::sentry _Ok(*this);
if (!_Ok) {
_State |= ios_base::badbit;
} else {
_TRY_IO_BEGIN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

God I really don't like these, it's pre-existing (and changing it would be ... a project) so don't consider this a change request, but maybe we should open an issue.

if (!_Sync_buf.emit()) {
_State |= ios_base::badbit;
}
_CATCH_IO_END
}
_Mybase::setstate(_State);
}
_NODISCARD streambuf_type* get_wrapped() const noexcept {
return _Sync_buf.get_wrapped();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <assert.h>
#include <ios>
#include <memory>
#include <sstream>
#include <streambuf>
#include <string>
#include <syncstream>
Expand Down Expand Up @@ -35,7 +36,8 @@ class string_buffer : public basic_streambuf<Ty, char_traits<Ty>> { // represent


template <class Ty, class Alloc = void, bool ThrowOnSync = false>
void test_osyncstream_manipulators(string_buffer<typename Ty::char_type, ThrowOnSync>* buf = nullptr) {
void test_osyncstream_manipulators(
string_buffer<typename Ty::char_type, ThrowOnSync>* buf = nullptr, bool buffer_can_sync = true) {
using char_type = typename Ty::char_type;
using traits_type = typename Ty::traits_type;

Expand All @@ -56,9 +58,9 @@ void test_osyncstream_manipulators(string_buffer<typename Ty::char_type, ThrowOn
assert(addressof(flush_emit(os)) == addressof(os));
if constexpr (is_base_of_v<basic_osyncstream<char_type, traits_type, Alloc>, Ty>) {
if constexpr (ThrowOnSync) {
assert(os.rdstate() == ios::badbit);
assert(os.rdstate() == ios_base::badbit);
} else {
assert(os.rdstate() == (buf ? ios::goodbit : ios::badbit));
assert(os.rdstate() == (buf ? ios_base::goodbit : ios_base::badbit));
fsb4000 marked this conversation as resolved.
Show resolved Hide resolved
}

if (buf) {
Expand All @@ -77,7 +79,8 @@ void test_osyncstream_manipulators(string_buffer<typename Ty::char_type, ThrowOn

assert(addressof(flush_emit(os)) == addressof(os));
if constexpr (is_base_of_v<basic_osyncstream<char_type, traits_type, Alloc>, Ty>) {
assert(os.rdstate() == ios::goodbit);
const auto state = (buf && buffer_can_sync) ? ios_base::goodbit : ios_base::badbit;
assert(os.rdstate() == state);
if (buf) {
assert(buf->str == "Another input");
}
Expand All @@ -89,6 +92,76 @@ void test_osyncstream_manipulators(string_buffer<typename Ty::char_type, ThrowOn
}
}

template <class Ty>
class throwing_string_buffer : public basic_streambuf<Ty, char_traits<Ty>> {
public:
throwing_string_buffer() = default;
~throwing_string_buffer() = default;

streamsize xsputn(const Ty*, streamsize) override {
throw ios_base::failure{"test exception"};
}
};

void test_lwg_3571() {
// LWG-3571: "flush_emit should set badbit if the emit call fails"
{
osyncstream stream(nullptr);
stream << flush_emit;
assert(stream.rdstate() == ios_base::badbit);
}
{
stringstream inner;
osyncstream stream(inner);
stream << "Hello World";
stream.setstate(ios_base::failbit);
assert((stream.rdstate() & ios_base::badbit) == ios_base::goodbit);
stream << flush_emit;
assert(stream.rdstate() & ios_base::badbit);
assert(inner.str() == "");
}
{
throwing_string_buffer<char> inner;
osyncstream stream(&inner);
stream << "Hello World";
stream.exceptions(ios_base::failbit | ios_base::badbit);
assert((stream.rdstate() & ios_base::badbit) == ios_base::goodbit);
try {
stream << flush_emit;
assert(false);
} catch (const ios_base::failure&) {
assert(stream.rdstate() & ios_base::badbit);
}
}
}

void test_lwg_3570() {
// LWG-3570: "basic_osyncstream::emit should be an unformatted output function"
{
stringstream inner;
osyncstream stream(inner);
stream << "Hello World";
stream.setstate(ios_base::failbit);
assert((stream.rdstate() & ios_base::badbit) == ios_base::goodbit);
stream.emit();
assert(stream.rdstate() & ios_base::badbit);
assert(inner.str() == "");
}
{
throwing_string_buffer<char> inner;
osyncstream stream(&inner);
stream << "Hello World";
stream.exceptions(ios_base::failbit | ios_base::badbit);
assert((stream.rdstate() & ios_base::badbit) == ios_base::goodbit);
try {
stream.emit();
assert(false);
} catch (const ios_base::failure&) {
assert(stream.rdstate() & ios_base::badbit);
}
}
}

int main() {
string_buffer<char> char_buffer{};
string_buffer<char, true> no_sync_char_buffer{};
Expand All @@ -102,5 +175,8 @@ int main() {

test_osyncstream_manipulators<basic_ostream<char>>(&no_sync_char_buffer);
test_osyncstream_manipulators<basic_osyncstream<char, char_traits<char>, allocator<char>>, allocator<char>>(
&no_sync_char_buffer);
&no_sync_char_buffer, false);

test_lwg_3571();
test_lwg_3570();
}