fix undefined behavior in VecDeque::splice

This commit is contained in:
Jeremy Smart 2026-01-27 19:30:37 -05:00
parent e96bb7e44f
commit b0d96492d0
No known key found for this signature in database
GPG key ID: 5AD8086D5DF29A8F
2 changed files with 16 additions and 1 deletions

View file

@ -143,7 +143,11 @@ impl<T, A: Allocator> Drain<'_, T, A> {
let new_tail_start = tail_start + additional;
unsafe {
deque.wrap_copy(tail_start, new_tail_start, self.tail_len);
deque.wrap_copy(
deque.to_physical_idx(tail_start),
deque.to_physical_idx(new_tail_start),
self.tail_len,
);
}
self.drain_len += additional;
}

View file

@ -2336,3 +2336,14 @@ fn test_splice_forget() {
std::mem::forget(v.splice(2..4, a));
assert_eq!(v, &[1, 2]);
}
#[test]
fn test_splice_wrapping() {
let mut vec = VecDeque::with_capacity(10);
vec.push_front(7u8);
vec.push_back(9);
vec.splice(1..1, [8]);
assert_eq!(Vec::from(vec), [7, 8, 9]);
}