From 99f5fc937f1042b24188a92bf2fe40f151d023bb Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 22 Jun 2015 15:01:21 +0000 Subject: [PATCH] Make seeking on an ostream that has eofbit set work correctly. Fixes PR#21361 llvm-svn: 240286 --- libcxx/include/ostream | 4 ++-- .../output.streams/ostream.seeks/seekp.pass.cpp | 11 +++++++++++ .../output.streams/ostream.seeks/seekp2.pass.cpp | 11 +++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/libcxx/include/ostream b/libcxx/include/ostream index a7af2994b6b9..f55fd40856ec 100644 --- a/libcxx/include/ostream +++ b/libcxx/include/ostream @@ -1004,7 +1004,7 @@ basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) { sentry __s(*this); - if (__s) + if (!this->fail()) { if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1)) this->setstate(ios_base::failbit); @@ -1018,7 +1018,7 @@ basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) { sentry __s(*this); - if (__s) + if (!this->fail()) { if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1)) this->setstate(ios_base::failbit); diff --git a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp.pass.cpp b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp.pass.cpp index e09627b29b64..ec3fe48866c7 100644 --- a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp.pass.cpp +++ b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp.pass.cpp @@ -40,11 +40,13 @@ protected: int main() { { + seekpos_called = 0; std::ostream os((std::streambuf*)0); assert(&os.seekp(5) == &os); assert(seekpos_called == 0); } { + seekpos_called = 0; testbuf sb; std::ostream os(&sb); assert(&os.seekp(10) == &os); @@ -54,4 +56,13 @@ int main() assert(seekpos_called == 2); assert(os.fail()); } + { // See https://llvm.org/bugs/show_bug.cgi?id=21361 + seekpos_called = 0; + testbuf sb; + std::ostream os(&sb); + os.setstate(std::ios_base::eofbit); + assert(&os.seekp(10) == &os); + assert(seekpos_called == 1); + assert(os.rdstate() == std::ios_base::eofbit); + } } diff --git a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp index 69b26f3a96e8..ebfd24af91d5 100644 --- a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp +++ b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.seeks/seekp2.pass.cpp @@ -42,11 +42,13 @@ protected: int main() { { + seekoff_called = 0; std::ostream os((std::streambuf*)0); assert(&os.seekp(5, std::ios_base::beg) == &os); assert(seekoff_called == 0); } { + seekoff_called = 0; testbuf sb; std::ostream os(&sb); assert(&os.seekp(10, std::ios_base::beg) == &os); @@ -56,4 +58,13 @@ int main() assert(seekoff_called == 2); assert(os.fail()); } + { // See https://llvm.org/bugs/show_bug.cgi?id=21361 + seekoff_called = 0; + testbuf sb; + std::ostream os(&sb); + os.setstate(std::ios_base::eofbit); + assert(&os.seekp(10, std::ios_base::beg) == &os); + assert(seekoff_called == 1); + assert(os.rdstate() == std::ios_base::eofbit); + } }