Skip to content

Commit

Permalink
Properly test allocator awareness of string
Browse files Browse the repository at this point in the history
Did fix some bugs in string

* We were only allocating _Newcapacity in _Copy_assign. This lead to memory corruption
* We were not calling _Tidy_deallocate in _Move_assign which leaked memory of lhs
* We did not swap proxies in swap if one of the strings was in SSO. Breaks with stateful allocators
* We always performed work in swap even if self swapping
  • Loading branch information
miscco committed Aug 27, 2021
1 parent a5e7657 commit c164242
Show file tree
Hide file tree
Showing 2 changed files with 436 additions and 53 deletions.
31 changes: 21 additions & 10 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -2871,7 +2871,7 @@ public:
}

private:
void _Memcpy_val_from(const basic_string& _Right) noexcept {
_CONSTEXPR20 void _Memcpy_val_from(const basic_string& _Right) noexcept {
_STL_INTERNAL_CHECK(_Can_memcpy_val); // TRANSITION, if constexpr
const auto _My_data_mem =
reinterpret_cast<unsigned char*>(_STD addressof(_Mypair._Myval2)) + _Memcpy_val_offset;
Expand Down Expand Up @@ -2914,6 +2914,11 @@ private:
_Right_data._Bx._Ptr = nullptr;
_Swap_proxy_and_iterators(_Right);
} else { // copy small string buffer
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
_Construct_in_place(_Mypair._Myval2._Bx);
}
#endif // _HAS_CXX20
_Traits::copy(_My_data._Bx._Buf, _Right_data._Bx._Buf, _Right_data._Mysize + 1);
_Right_data._Orphan_all();
}
Expand Down Expand Up @@ -3017,19 +3022,25 @@ public:
static constexpr auto npos{static_cast<size_type>(-1)};

private:
void _Copy_assign_val_from_small(const basic_string& _Right) {
_CONSTEXPR20 void _Copy_assign_val_from_small(const basic_string& _Right) {
// TRANSITION, VSO-761321; inline into only caller when that's fixed
_Tidy_deallocate();
if constexpr (_Can_memcpy_val) {
_Memcpy_val_from(_Right);
} else {
auto& _My_data = _Mypair._Myval2;
auto& _Right_data = _Right._Mypair._Myval2;

_Traits::copy(_My_data._Bx._Buf, _Right_data._Bx._Buf, _Right_data._Mysize + 1);
_My_data._Mysize = _Right_data._Mysize;
_My_data._Myres = _Right_data._Myres;
#if _HAS_CXX20
if (!is_constant_evaluated())
#endif // _HAS_CXX20
{
_Memcpy_val_from(_Right);
return;
}
}

auto& _My_data = _Mypair._Myval2;
auto& _Right_data = _Right._Mypair._Myval2;

_Traits::copy(_My_data._Bx._Buf, _Right_data._Bx._Buf, _Right_data._Mysize + 1);
_My_data._Mysize = _Right_data._Mysize;
_My_data._Myres = _Right_data._Myres;
}

_CONSTEXPR20 void _Copy_assign(const basic_string& _Right, false_type) {
Expand Down
Loading

0 comments on commit c164242

Please sign in to comment.