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

Piotr Fusik via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 1 03:17:58 PDT 2023


pfusik created this revision.
Herald added a project: All.
pfusik requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156783

Files:
  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


Index: libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp
===================================================================
--- libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp
+++ libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp
@@ -31,6 +31,12 @@
     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**) {
Index: libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.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/stringbuf/stringbuf.members/str.move.pass.cpp
@@ -31,6 +31,12 @@
     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**) {
Index: libcxx/test/std/input.output/string.streams/ostringstream/ostringstream.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/ostringstream/ostringstream.members/str.move.pass.cpp
@@ -31,6 +31,12 @@
     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**) {
Index: libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp
===================================================================
--- libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp
+++ libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp
@@ -31,6 +31,12 @@
     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**) {
Index: libcxx/include/sstream
===================================================================
--- libcxx/include/sstream
+++ libcxx/include/sstream
@@ -399,8 +399,10 @@
     _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())
+            __result.assign(std::move(__str_), __view.data() - __str_.data(), __view.size());
         __str_.clear();
         __init_buf_ptrs();
         return __result;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156783.545989.patch
Type: text/x-patch
Size: 3337 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230801/83fac5ce/attachment.bin>


More information about the libcxx-commits mailing list