Skip to content

Commit f418cb1

Browse files
committed
[libc++] Fix std::out_of_range thrown from basic_stringbuf::str() &&
Reviewed By: #libc, Mordante, philnik Differential Revision: https://reviews.llvm.org/D156783
1 parent 8066d61 commit f418cb1

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

libcxx/include/sstream

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,12 @@ public:
399399
_LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const & { return str(__str_.get_allocator()); }
400400

401401
_LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && {
402+
string_type __result;
402403
const basic_string_view<_CharT, _Traits> __view = view();
403-
string_type __result(std::move(__str_), __view.data() - __str_.data(), __view.size());
404+
if (!__view.empty()) {
405+
auto __pos = __view.data() - __str_.data();
406+
__result.assign(std::move(__str_), __pos, __view.size());
407+
}
404408
__str_.clear();
405409
__init_buf_ptrs();
406410
return __result;

libcxx/test/std/input.output/string.streams/istringstream/istringstream.members/str.move.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ static void test() {
3131
assert(s == STR("testing"));
3232
assert(ss.view().empty());
3333
}
34+
{
35+
std::basic_istringstream<CharT> ss;
36+
std::basic_string<CharT> s = std::move(ss).str();
37+
assert(s.empty());
38+
assert(ss.view().empty());
39+
}
3440
}
3541

3642
int main(int, char**) {

libcxx/test/std/input.output/string.streams/ostringstream/ostringstream.members/str.move.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ static void test() {
3131
assert(s == STR("testing"));
3232
assert(ss.view().empty());
3333
}
34+
{
35+
std::basic_ostringstream<CharT> ss;
36+
std::basic_string<CharT> s = std::move(ss).str();
37+
assert(s.empty());
38+
assert(ss.view().empty());
39+
}
3440
}
3541

3642
int main(int, char**) {

libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.members/str.move.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ static void test() {
3131
assert(s == STR("testing"));
3232
assert(buf.view().empty());
3333
}
34+
{
35+
std::basic_stringbuf<CharT> buf;
36+
std::basic_string<CharT> s = std::move(buf).str();
37+
assert(s.empty());
38+
assert(buf.view().empty());
39+
}
3440
}
3541

3642
int main(int, char**) {

libcxx/test/std/input.output/string.streams/stringstream/stringstream.members/str.move.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ static void test() {
3131
assert(s == STR("testing"));
3232
assert(ss.view().empty());
3333
}
34+
{
35+
std::basic_stringstream<CharT> ss;
36+
std::basic_string<CharT> s = std::move(ss).str();
37+
assert(s.empty());
38+
assert(ss.view().empty());
39+
}
3440
}
3541

3642
int main(int, char**) {

0 commit comments

Comments
 (0)