Skip to content

Commit 3ffbf6e

Browse files
Rollup merge of rust-lang#151769 - Qelxiros:vecdeque_splice_fix, r=joboet
fix undefined behavior in VecDeque::splice closes rust-lang#151758
2 parents b841ab2 + b0d9649 commit 3ffbf6e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ impl<T, A: Allocator> Drain<'_, T, A> {
143143

144144
let new_tail_start = tail_start + additional;
145145
unsafe {
146-
deque.wrap_copy(tail_start, new_tail_start, self.tail_len);
146+
deque.wrap_copy(
147+
deque.to_physical_idx(tail_start),
148+
deque.to_physical_idx(new_tail_start),
149+
self.tail_len,
150+
);
147151
}
148152
self.drain_len += additional;
149153
}

library/alloctests/tests/vec_deque.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,3 +2336,14 @@ fn test_splice_forget() {
23362336
std::mem::forget(v.splice(2..4, a));
23372337
assert_eq!(v, &[1, 2]);
23382338
}
2339+
2340+
#[test]
2341+
fn test_splice_wrapping() {
2342+
let mut vec = VecDeque::with_capacity(10);
2343+
vec.push_front(7u8);
2344+
vec.push_back(9);
2345+
2346+
vec.splice(1..1, [8]);
2347+
2348+
assert_eq!(Vec::from(vec), [7, 8, 9]);
2349+
}

0 commit comments

Comments
 (0)