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

Fix deque's usage of _Allocate_at_least_helper #4017

Merged
Merged
Show file tree
Hide file tree
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
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");
Copy link
Contributor Author

@achabense achabense Sep 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put debug check in _Allocate_at_least_helper instead. (And is _STL_ASSERT a suitable choice for such check?)

Copy link
Member

@CaseyCarter CaseyCarter Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of _STL_ASSERT is fine, but the check really belongs in _Allocate_at_least_helper with an error message like "your allocate_at_least is broken: it didn't allocate "at least"".

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};
Copy link
Contributor Author

@achabense achabense Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count * 2 just happened to work; the deque will break easily if users try to provide their own allocate_at_least.

}

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