Skip to content

[libc++] Fixes istream::sync. #76467

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

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions libcxx/include/istream
Original file line number Diff line number Diff line change
Expand Up @@ -1009,17 +1009,18 @@ basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
template <class _CharT, class _Traits>
int basic_istream<_CharT, _Traits>::sync() {
ios_base::iostate __state = ios_base::goodbit;
int __r = 0;
sentry __sen(*this, true);
if (this->rdbuf() == nullptr)
return -1;

int __r = 0;
if (__sen) {
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try {
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
if (this->rdbuf() == nullptr)
return -1;
if (this->rdbuf()->pubsync() == -1) {
__state |= ios_base::badbit;
return -1;
__r = -1;
}
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
} catch (...) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

// int sync();

// The fix for bug 51497 and bug 51499 require and updated dylib due to
// explicit instantiations. That means Apple backdeployment targets remain
// broken.
// TODO(#82107) Enable XFAIL.
// UNSUPPORTED: using-built-library-before-llvm-19

#include <istream>
#include <cassert>

Expand Down Expand Up @@ -48,6 +54,18 @@ struct testbuf
}
};

template <class CharT>
struct testbuf_pubsync_error
: public std::basic_streambuf<CharT>
{
public:

testbuf_pubsync_error() {}
protected:
virtual int sync() { return -1; }
};


#ifndef TEST_HAS_NO_EXCEPTIONS
struct testbuf_exception { };

Expand Down Expand Up @@ -85,21 +103,62 @@ struct throwing_testbuf

int main(int, char**)
{
{
std::istream is(nullptr);
assert(is.sync() == -1);
}
{
testbuf<char> sb(" 123456789");
std::istream is(&sb);
assert(is.sync() == 0);
assert(sync_called == 1);
}
{
testbuf_pubsync_error<char> sb;
std::istream is(&sb);
is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);
assert(is.sync() == -1);
assert( is.bad());
assert(!is.eof());
assert( is.fail());
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
std::wistream is(nullptr);
assert(is.sync() == -1);
}
{
testbuf<wchar_t> sb(L" 123456789");
std::wistream is(&sb);
assert(is.sync() == 0);
assert(sync_called == 2);
}
{
testbuf_pubsync_error<wchar_t> sb;
std::wistream is(&sb);
is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);
assert(is.sync() == -1);
assert( is.bad());
assert(!is.eof());
assert( is.fail());
}
#endif
#ifndef TEST_HAS_NO_EXCEPTIONS
{
testbuf_pubsync_error<char> sb;
std::istream is(&sb);
is.exceptions(std::ios_base::badbit);
bool threw = false;
try {
is.sync();
} catch (std::ios_base::failure const&) {
threw = true;
}
assert( is.bad());
assert(!is.eof());
assert( is.fail());
assert(threw);
}
{
throwing_testbuf<char> sb(" 123456789");
std::basic_istream<char> is(&sb);
Expand All @@ -116,6 +175,21 @@ int main(int, char**)
assert(threw);
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
testbuf_pubsync_error<wchar_t> sb;
std::wistream is(&sb);
is.exceptions(std::ios_base::badbit);
bool threw = false;
try {
is.sync();
} catch (std::ios_base::failure const&) {
threw = true;
}
assert( is.bad());
assert(!is.eof());
assert( is.fail());
assert(threw);
}
{
throwing_testbuf<wchar_t> sb(L" 123456789");
std::basic_istream<wchar_t> is(&sb);
Expand All @@ -131,7 +205,7 @@ int main(int, char**)
assert( is.fail());
assert(threw);
}
#endif
#endif // TEST_HAS_NO_WIDE_CHARACTERS
#endif // TEST_HAS_NO_EXCEPTIONS

return 0;
Expand Down