Github Fix overflowing length in Vec<ZST> to VecDeque by cuviper · Pull Re...
source link: https://github.com/rust-lang/rust/pull/83244
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Vec
can hold up to usize::MAX
ZST items, but VecDeque
has a lower
limit to keep its raw capacity as a power of two, so we should check
that in From<Vec<T>> for VecDeque<T>
. We can also simplify the
capacity check for the remaining non-ZST case.
Before this fix, the new test would fail on the length:
thread 'collections::vec_deque::tests::test_from_vec_zst_overflow' panicked at 'assertion failed: `(left == right)`
left: `0`,
right: `9223372036854775808`', library/alloc/src/collections/vec_deque/tests.rs:474:5
note: panic did not contain expected string
panic message: `"assertion failed: `(left == right)`\n left: `0`,\n right: `9223372036854775808`"`,
expected substring: `"capacity overflow"`
That was a result of len()
using a mask & (size - 1)
with the
improper length. Now we do get a "capacity overflow" panic as soon as
that VecDeque::from(vec)
is attempted.
Fixes #80167.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK