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 2 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
5 changes: 4 additions & 1 deletion stl/inc/ostream
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,10 @@ 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();
const bool _Emit_failed = !_Sync_buf_ptr->_Do_emit();
if (_Emit_failed) {
_Ostr.setstate(ios_base::badbit);
}
fsb4000 marked this conversation as resolved.
Show resolved Hide resolved
}
return _Ostr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 Down Expand Up @@ -77,7 +78,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::goodbit : ios::badbit;
assert(os.rdstate() == state);
if (buf) {
assert(buf->str == "Another input");
}
Expand All @@ -89,6 +91,12 @@ void test_osyncstream_manipulators(string_buffer<typename Ty::char_type, ThrowOn
}
}

void test_lwg_3571() {
basic_osyncstream<char> stream(nullptr);
stream << flush_emit;
assert(stream.rdstate() == ios::badbit);
}

int main() {
string_buffer<char> char_buffer{};
string_buffer<char, true> no_sync_char_buffer{};
Expand All @@ -102,5 +110,7 @@ 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();
}