Skip to content

Commit 093a53f

Browse files
authored
Rollup merge of #108462 - pommicket:fix-vecdeque-zst-overflow, r=Amanieu
Fix `VecDeque::append` capacity overflow for ZSTs Fixes #108454.
2 parents 27fa7b5 + 12f959b commit 093a53f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

library/alloc/src/collections/vec_deque/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
19241924
#[stable(feature = "append", since = "1.4.0")]
19251925
pub fn append(&mut self, other: &mut Self) {
19261926
if T::IS_ZST {
1927-
self.len += other.len;
1927+
self.len = self.len.checked_add(other.len).expect("capacity overflow");
19281928
other.len = 0;
19291929
other.head = 0;
19301930
return;

library/alloc/tests/vec_deque.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,20 @@ fn test_append_double_drop() {
10451045
assert_eq!(count_b, 1);
10461046
}
10471047

1048+
#[test]
1049+
#[should_panic]
1050+
fn test_append_zst_capacity_overflow() {
1051+
let mut v = Vec::with_capacity(usize::MAX);
1052+
// note: using resize instead of set_len here would
1053+
// be *extremely* slow in unoptimized builds.
1054+
// SAFETY: `v` has capacity `usize::MAX`, and no initialization
1055+
// is needed for empty tuples.
1056+
unsafe { v.set_len(usize::MAX) };
1057+
let mut v = VecDeque::from(v);
1058+
let mut w = vec![()].into();
1059+
v.append(&mut w);
1060+
}
1061+
10481062
#[test]
10491063
fn test_retain() {
10501064
let mut buf = VecDeque::new();

0 commit comments

Comments
 (0)