[libcxx-commits] [libcxx] f418cb1 - [libc++] Fix `std::out_of_range` thrown from `basic_stringbuf::str() &&`

Piotr Fusik via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 2 07:26:57 PDT 2023


Author: Piotr Fusik
Date: 2023-08-02T16:26:51+02:00
New Revision: f418cb1a9367d85c7c9b1aa93dc3fa60c8ef9849

URL: https://github.com/llvm/llvm-project/commit/f418cb1a9367d85c7c9b1aa93dc3fa60c8ef9849
DIFF: https://github.com/llvm/llvm-project/commit/f418cb1a9367d85c7c9b1aa93dc3fa60c8ef9849.diff

LOG: [libc++] Fix `std::out_of_range` thrown from `basic_stringbuf::str() &&`

Reviewed By: #libc, Mordante, philnik

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

Added: 
    

Modified: 
    libcxx/include/sstream
    libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp
    libcxx/test/std/input.output/string.streams/ostringstream/ostringstream.members/str.move.pass.cpp
    libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.members/str.move.pass.cpp
    libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/sstream b/libcxx/include/sstream
index d7ad0213eb3499..81255c878f7f3a 100644
--- a/libcxx/include/sstream
+++ b/libcxx/include/sstream
@@ -399,8 +399,12 @@ public:
     _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const & { return str(__str_.get_allocator()); }
 
     _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && {
+        string_type __result;
         const basic_string_view<_CharT, _Traits> __view = view();
-        string_type __result(std::move(__str_), __view.data() - __str_.data(), __view.size());
+        if (!__view.empty()) {
+            auto __pos = __view.data() - __str_.data();
+            __result.assign(std::move(__str_), __pos, __view.size());
+        }
         __str_.clear();
         __init_buf_ptrs();
         return __result;

diff  --git a/libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp b/libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp
index 7c8cf2c713db70..546f82166aaefa 100644
--- a/libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp
+++ b/libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp
@@ -31,6 +31,12 @@ static void test() {
     assert(s == STR("testing"));
     assert(ss.view().empty());
   }
+  {
+    std::basic_istringstream<CharT> ss;
+    std::basic_string<CharT> s = std::move(ss).str();
+    assert(s.empty());
+    assert(ss.view().empty());
+  }
 }
 
 int main(int, char**) {

diff  --git a/libcxx/test/std/input.output/string.streams/ostringstream/ostringstream.members/str.move.pass.cpp b/libcxx/test/std/input.output/string.streams/ostringstream/ostringstream.members/str.move.pass.cpp
index 5fbdf81f0c08ec..57f2384bae52c6 100644
--- a/libcxx/test/std/input.output/string.streams/ostringstream/ostringstream.members/str.move.pass.cpp
+++ b/libcxx/test/std/input.output/string.streams/ostringstream/ostringstream.members/str.move.pass.cpp
@@ -31,6 +31,12 @@ static void test() {
     assert(s == STR("testing"));
     assert(ss.view().empty());
   }
+  {
+    std::basic_ostringstream<CharT> ss;
+    std::basic_string<CharT> s = std::move(ss).str();
+    assert(s.empty());
+    assert(ss.view().empty());
+  }
 }
 
 int main(int, char**) {

diff  --git a/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.members/str.move.pass.cpp b/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.members/str.move.pass.cpp
index f0fa330b4b600f..0f0f540a9c2474 100644
--- a/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.members/str.move.pass.cpp
+++ b/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.members/str.move.pass.cpp
@@ -31,6 +31,12 @@ static void test() {
     assert(s == STR("testing"));
     assert(buf.view().empty());
   }
+  {
+    std::basic_stringbuf<CharT> buf;
+    std::basic_string<CharT> s = std::move(buf).str();
+    assert(s.empty());
+    assert(buf.view().empty());
+  }
 }
 
 int main(int, char**) {

diff  --git a/libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp b/libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp
index a6b8ec6c37ebaf..35349c9c288ec1 100644
--- a/libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp
+++ b/libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp
@@ -31,6 +31,12 @@ static void test() {
     assert(s == STR("testing"));
     assert(ss.view().empty());
   }
+  {
+    std::basic_stringstream<CharT> ss;
+    std::basic_string<CharT> s = std::move(ss).str();
+    assert(s.empty());
+    assert(ss.view().empty());
+  }
 }
 
 int main(int, char**) {


        


More information about the libcxx-commits mailing list