Skip to content

Commit

Permalink
Fix deque's usage of _Allocate_at_least_helper (#4017)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
achabense and StephanTLavavej authored Sep 21, 2023
1 parent acb342c commit 2ae7622
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion stl/inc/deque
Original file line number Diff line number Diff line change
Expand Up @@ -1557,9 +1557,15 @@ private:
_Newsize *= 2;
}

size_type _Allocsize = _Newsize;

size_type _Myboff = _Myoff() / _Block_size;
_Mapptr _Newmap = _Allocate_at_least_helper(_Almap, _Newsize);
_Mapptr _Newmap = _Allocate_at_least_helper(_Almap, _Allocsize);
_Mapptr _Myptr = _Newmap + _Myboff;
_STL_ASSERT(_Allocsize >= _Newsize, "_Allocsize >= _Newsize");
while (_Newsize <= _Allocsize / 2) {
_Newsize *= 2;
}

_Count = _Newsize - _Mapsize();

Expand Down
11 changes: 8 additions & 3 deletions tests/std/tests/GH_003570_allocate_at_least/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct signalling_allocator {

allocation_result<T*> allocate_at_least(size_t count) {
allocate_at_least_signal.set();
return {allocate(count * 2), count * 2};
return {allocate(count * 2 + 1), count * 2 + 1};
}

void deallocate(T* ptr, size_t) noexcept {
Expand All @@ -72,10 +72,15 @@ void test_container() {
}

void test_deque() {
deque<int, signalling_allocator<int>> d;
d.resize(100);
deque<size_t, signalling_allocator<size_t>> d;
for (size_t i = 0; i < 100; ++i) {
d.push_back(i);
}
assert(allocate_at_least_signal.consume());
assert(d.size() == 100);
for (size_t i = 0; i < 100; ++i) {
assert(d[i] == i);
}
}

void test_stream_overflow(auto& stream) {
Expand Down

0 comments on commit 2ae7622

Please sign in to comment.