Skip to content

Commit

Permalink
Rollup merge of rust-lang#90010 - rusticstuff:vecdeque_with_capacity_…
Browse files Browse the repository at this point in the history
…in_overflow, r=m-ou-se

Avoid overflow in `VecDeque::with_capacity_in()`.

The overflow only happens if alloc is compiled with overflow checks enabled and the passed capacity is greater or equal 2^(usize::BITS-1). The overflow shadows the expected "capacity overflow" panic leading to a test failure if overflow checks are enabled for std in the CI.

Unblocks [CI: Enable overflow checks for test (non-dist) builds rust-lang#89776](rust-lang#89776).

For some reason the overflow is only observable with optimization turned off, but that is a separate issue.
  • Loading branch information
JohnTitor committed Oct 21, 2021
2 parents 504d16a + 4a37b9c commit 9b5f471
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> {
assert!(capacity < 1_usize << usize::BITS - 1, "capacity overflow");
// +1 since the ringbuffer always leaves one space empty
let cap = cmp::max(capacity + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
assert!(cap > capacity, "capacity overflow");

VecDeque { tail: 0, head: 0, buf: RawVec::with_capacity_in(cap, alloc) }
}
Expand Down

0 comments on commit 9b5f471

Please sign in to comment.