[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 11:22:41 PDT 2023


pfusik updated this revision to Diff 546158.
pfusik marked an inline comment as done.
pfusik added a comment.

Refactor a function argument to a variable (NFC).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156783/new/

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,12 @@
     _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;


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


More information about the libcxx-commits mailing list