-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fix overflow when converting ZST Vec to VecDeque #80003
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 zero 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just open a new issue👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now create a 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) | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.