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 overflow when converting ZST Vec to VecDeque #80003

Merged
merged 3 commits into from
Dec 18, 2020
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: 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maximum len of ZST Vec is larger than maximum len of ZST VecDeque (MAXIMUM_ZST_CAPACITY - 1), which would be another thing to check before constructing VecDeque.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if this should be changed in this PR, but it's definitely a check that should be added I think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair. I mentioned it here, because it wasn't an issue so far, but will be as a result of changes in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

You can just open a new issue👍

Copy link
Contributor

Choose a reason for hiding this comment

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

We now create a VecDeque with a capacity which is not a power of two here, breaking one of its invariants.

This is either unsound or that invariant isn't actually required anywhere and we should remove it.

|| (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);
}