Skip to content

Commit

Permalink
Rollup merge of rust-lang#80003 - Stupremee:fix-zst-vecdeque-conversi…
Browse files Browse the repository at this point in the history
…on-panic, r=dtolnay

Fix overflow when converting ZST Vec to VecDeque

```rust
let v = vec![(); 100];
let queue = VecDeque::from(v);
println!("{:?}", queue);
```
This code will currently panic with a capacity overflow.
This PR resolves this issue and makes the code run fine.

Resolves rust-lang#78532
  • Loading branch information
Dylan-DPC authored Dec 16, 2020
2 parents a674cb3 + 09d528e commit 642ba23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2793,8 +2793,12 @@ impl<T> From<Vec<T>> for VecDeque<T> {
let len = other.len();

// We need to extend the buf if it's not a power of two, too small
// or doesn't have at least one free space
if !buf.capacity().is_power_of_two()
// or doesn't have at least one free space.
// We check if `T` is a ZST in the first condition,
// because `usize::MAX` (the capacity returned by `capacity()` for ZST)
// is not a power of two and thus it'll always try
// to reserve more memory which will panic for ZST (rust-lang/rust#78532)
if (!buf.capacity().is_power_of_two() && mem::size_of::<T>() != 0)
|| (buf.capacity() < (MINIMUM_CAPACITY + 1))
|| (buf.capacity() == len)
{
Expand Down
7 changes: 7 additions & 0 deletions library/alloc/tests/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,3 +1728,10 @@ fn test_zero_sized_push() {
}
}
}

#[test]
fn test_from_zero_sized_vec() {
let v = vec![(); 100];
let queue = VecDeque::from(v);
assert_eq!(queue.len(), 100);
}

0 comments on commit 642ba23

Please sign in to comment.