[libcxx] r321124 - libcxx: Fix for basic_stringbuf::seekoff() after r320604.

Peter Collingbourne via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 19 15:33:16 PST 2017


Author: pcc
Date: Tue Dec 19 15:33:16 2017
New Revision: 321124

URL: http://llvm.org/viewvc/llvm-project?rev=321124&view=rev
Log:
libcxx: Fix for basic_stringbuf::seekoff() after r320604.

As a result of this change, the basic_stringbuf constructor that
takes a mode ends up leaving __hm_ set to 0, causing the comparison
"__hm_ - __str_.data() < __noff" in seekoff() to succeed, which caused
the function to incorrectly return -1. The fix is to account for the
possibility of __hm_ being 0 when computing the distance from __hm_
to the start of the string.

Differential Revision: https://reviews.llvm.org/D41319

Modified:
    libcxx/trunk/include/sstream
    libcxx/trunk/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp

Modified: libcxx/trunk/include/sstream
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/sstream?rev=321124&r1=321123&r2=321124&view=diff
==============================================================================
--- libcxx/trunk/include/sstream (original)
+++ libcxx/trunk/include/sstream Tue Dec 19 15:33:16 2017
@@ -577,6 +577,7 @@ basic_stringbuf<_CharT, _Traits, _Alloca
     if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
         && __way == ios_base::cur)
         return pos_type(-1);
+    const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data();
     off_type __noff;
     switch (__way)
     {
@@ -590,13 +591,13 @@ basic_stringbuf<_CharT, _Traits, _Alloca
             __noff = this->pptr() - this->pbase();
         break;
     case ios_base::end:
-        __noff = __hm_ - __str_.data();
+        __noff = __hm;
         break;
     default:
         return pos_type(-1);
     }
     __noff += __off;
-    if (__noff < 0 || __hm_ - __str_.data() < __noff)
+    if (__noff < 0 || __hm < __noff)
         return pos_type(-1);
     if (__noff != 0)
     {

Modified: libcxx/trunk/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp?rev=321124&r1=321123&r2=321124&view=diff
==============================================================================
--- libcxx/trunk/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp (original)
+++ libcxx/trunk/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp Tue Dec 19 15:33:16 2017
@@ -21,6 +21,30 @@
 int main()
 {
     {
+        std::stringbuf sb(std::ios_base::in);
+        assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == -1);
+        assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == -1);
+        assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == -1);
+        assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in | std::ios_base::out) == -1);
+        assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in | std::ios_base::out) == -1);
+        assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in | std::ios_base::out) == -1);
+        assert(sb.pubseekoff(0, std::ios_base::beg, std::ios_base::in) == 0);
+        assert(sb.pubseekoff(0, std::ios_base::cur, std::ios_base::in) == 0);
+        assert(sb.pubseekoff(0, std::ios_base::end, std::ios_base::in) == 0);
+    }
+    {
+        std::stringbuf sb(std::ios_base::out);
+        assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == -1);
+        assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == -1);
+        assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == -1);
+        assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in | std::ios_base::out) == -1);
+        assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in | std::ios_base::out) == -1);
+        assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in | std::ios_base::out) == -1);
+        assert(sb.pubseekoff(0, std::ios_base::beg, std::ios_base::out) == 0);
+        assert(sb.pubseekoff(0, std::ios_base::cur, std::ios_base::out) == 0);
+        assert(sb.pubseekoff(0, std::ios_base::end, std::ios_base::out) == 0);
+    }
+    {
         std::stringbuf sb("0123456789", std::ios_base::in);
         assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == -1);
         assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == -1);




More information about the cfe-commits mailing list