diff --git a/libcxx/include/istream b/libcxx/include/istream index 7284fc1fee06..14fa466057cf 100644 --- a/libcxx/include/istream +++ b/libcxx/include/istream @@ -1369,8 +1369,10 @@ basic_istream<_CharT, _Traits>::seekg(pos_type __pos) this->clear(this->rdstate() & ~ios_base::eofbit); sentry __sen(*this, true); if (__sen) + { if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1)) this->setstate(ios_base::failbit); + } #ifndef _LIBCPP_NO_EXCEPTIONS } catch (...) @@ -1391,7 +1393,10 @@ basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) #endif // _LIBCPP_NO_EXCEPTIONS sentry __sen(*this, true); if (__sen) - this->rdbuf()->pubseekoff(__off, __dir, ios_base::in); + { + if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1)) + this->setstate(ios_base::failbit); + } #ifndef _LIBCPP_NO_EXCEPTIONS } catch (...) diff --git a/libcxx/include/ostream b/libcxx/include/ostream index 2c618d41b9dd..041314acc756 100644 --- a/libcxx/include/ostream +++ b/libcxx/include/ostream @@ -1159,7 +1159,8 @@ inline _LIBCPP_INLINE_VISIBILITY basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) { - if (!this->fail()) + sentry __s(*this); + if (__s) { if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1)) this->setstate(ios_base::failbit); @@ -1172,8 +1173,12 @@ inline _LIBCPP_INLINE_VISIBILITY basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) { - if (!this->fail()) - this->rdbuf()->pubseekoff(__off, __dir, ios_base::out); + sentry __s(*this); + if (__s) + { + if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1)) + this->setstate(ios_base::failbit); + } return *this; } diff --git a/libcxx/test/input.output/iostream.format/input.streams/istream.unformatted/seekg_off.pass.cpp b/libcxx/test/input.output/iostream.format/input.streams/istream.unformatted/seekg_off.pass.cpp index 005b3b48446c..73f3da1c6bd1 100644 --- a/libcxx/test/input.output/iostream.format/input.streams/istream.unformatted/seekg_off.pass.cpp +++ b/libcxx/test/input.output/iostream.format/input.streams/istream.unformatted/seekg_off.pass.cpp @@ -57,12 +57,18 @@ int main() is.seekg(5, std::ios_base::cur); assert(is.good()); assert(seekoff_called == 1); + is.seekg(-1, std::ios_base::beg); + assert(is.fail()); + assert(seekoff_called == 2); } { testbuf sb(L" 123456789"); std::wistream is(&sb); is.seekg(5, std::ios_base::cur); assert(is.good()); - assert(seekoff_called == 2); + assert(seekoff_called == 3); + is.seekg(-1, std::ios_base::beg); + assert(is.fail()); + assert(seekoff_called == 4); } } diff --git a/libcxx/test/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp b/libcxx/test/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp index 16fbf6560769..69b26f3a96e8 100644 --- a/libcxx/test/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp +++ b/libcxx/test/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp @@ -54,6 +54,6 @@ int main() assert(os.good()); assert(&os.seekp(-1, std::ios_base::beg) == &os); assert(seekoff_called == 2); - assert(os.good()); + assert(os.fail()); } }