Rollup merge of #108462 - pommicket:fix-vecdeque-zst-overflow, r=Amanieu

Fix `VecDeque::append` capacity overflow for ZSTs

Fixes #108454.
This commit is contained in:
Dylan DPC 2023-03-01 23:40:20 +05:30 committed by GitHub
commit 093a53f134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -1924,7 +1924,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
#[stable(feature = "append", since = "1.4.0")]
pub fn append(&mut self, other: &mut Self) {
if T::IS_ZST {
self.len += other.len;
self.len = self.len.checked_add(other.len).expect("capacity overflow");
other.len = 0;
other.head = 0;
return;

View File

@ -1045,6 +1045,20 @@ fn test_append_double_drop() {
assert_eq!(count_b, 1);
}
#[test]
#[should_panic]
fn test_append_zst_capacity_overflow() {
let mut v = Vec::with_capacity(usize::MAX);
// note: using resize instead of set_len here would
// be *extremely* slow in unoptimized builds.
// SAFETY: `v` has capacity `usize::MAX`, and no initialization
// is needed for empty tuples.
unsafe { v.set_len(usize::MAX) };
let mut v = VecDeque::from(v);
let mut w = vec![()].into();
v.append(&mut w);
}
#[test]
fn test_retain() {
let mut buf = VecDeque::new();