Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't assume that _Hash_vec's size and capacity are always equal #2774

Merged
merged 3 commits into from
Jun 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions stl/inc/xhash
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ struct _Hash_vec {
_Aliter_traits::max_size(_Mypair._Get_first()));
}

_NODISCARD size_type capacity() const noexcept {
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
// This implementation never has capacity() differ from size(), but the previous implementation could.
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
// We need to handle that situation gracefully since we may link to old code (See GH-2774).
return static_cast<size_type>(_Mypair._Myval2._Myend - _Mypair._Myval2._Myfirst);
}

void _Assign_grow(const size_type _Cells, const value_type _Val) {
// set the elements stored here to _Cells copies of _Val, leaving the value unchanged if an exception is thrown
const auto _Oldsize = size();
Expand All @@ -277,9 +283,10 @@ struct _Hash_vec {
if (_Oldsize < _Cells) {
const auto _Newvec = _Alvec.allocate(_Cells); // throws
// nothrow hereafter
if (_Oldsize != 0) {
const auto _Oldcapacity = capacity();
if (_Oldcapacity != 0) {
_Destroy_range(_Mypair._Myval2._Myfirst, _Mypair._Myval2._Mylast);
_Alvec.deallocate(_Mypair._Myval2._Myfirst, _Oldsize);
_Alvec.deallocate(_Mypair._Myval2._Myfirst, _Oldcapacity);
}

_Mypair._Myval2._Myfirst = _Newvec;
Expand All @@ -294,7 +301,7 @@ struct _Hash_vec {

void _Tidy() noexcept {
_Destroy_range(_Mypair._Myval2._Myfirst, _Mypair._Myval2._Mylast);
_Mypair._Get_first().deallocate(_Mypair._Myval2._Myfirst, size());
_Mypair._Get_first().deallocate(_Mypair._Myval2._Myfirst, capacity());
_Mypair._Myval2._Myfirst = nullptr;
_Mypair._Myval2._Mylast = nullptr;
_Mypair._Myval2._Myend = nullptr;
Expand Down