summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-12-18 00:30:11 +0100
committerGitHub <noreply@github.com>2020-12-18 00:30:11 +0100
commit2e9ed6fb936f422bbb6be70468d8cd6b9b23495c (patch)
tree02127dabaa25feaf290eb339596615533312ce11 /library/alloc/src
parent720b6941dff18896bcab36bec8e905fdb462d7d8 (diff)
parent09d528ec155f77349001fa7eb6c3ae3363f41412 (diff)
downloadrust-2e9ed6fb936f422bbb6be70468d8cd6b9b23495c.tar.gz
rust-2e9ed6fb936f422bbb6be70468d8cd6b9b23495c.zip
Rollup merge of #80003 - Stupremee:fix-zst-vecdeque-conversion-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 #78532
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/collections/vec_deque/mod.rs8
1 files changed, 6 insertions, 2 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs
index 57807bc5453..9e54c15ea6a 100644
--- a/library/alloc/src/collections/vec_deque/mod.rs
+++ b/library/alloc/src/collections/vec_deque/mod.rs
@@ -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)
             {