Skip to content

Commit 47014b1

Browse files
committed
Don't do pointer arithmetic on pointers to deallocated memory
vec::Splice can invalidate the slice::Iter inside vec::Drain. So we replace them with dangling pointers which, unlike ones to deallocated memory, are allowed.
1 parent 4817259 commit 47014b1

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

library/alloc/src/vec/drain.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
223223
}
224224

225225
// as_slice() must only be called when iter.len() is > 0 because
226-
// vec::Splice modifies vec::Drain fields and may grow the vec which would invalidate
227-
// the iterator's internal pointers. Creating a reference to deallocated memory
228-
// is invalid even when it is zero-length
226+
// it also gets touched by vec::Splice which may turn it into a dangling pointer
227+
// which would make it and the vec pointer point to different allocations which would
228+
// lead to invalid pointer arithmetic below.
229229
let drop_ptr = iter.as_slice().as_ptr();
230230

231231
unsafe {

library/alloc/src/vec/splice.rs

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> {}
5454
impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> {
5555
fn drop(&mut self) {
5656
self.drain.by_ref().for_each(drop);
57+
// At this point draining is done and the only remaining tasks are splicing
58+
// and moving things into the final place.
59+
// Which means we can replace the slice::Iter with pointers that won't point to deallocated
60+
// memory, so that Drain::drop is still allowed to call iter.len(), otherwise it would break
61+
// the ptr.sub_ptr contract.
62+
self.drain.iter = (&[]).iter();
5763

5864
unsafe {
5965
if self.drain.tail_len == 0 {

0 commit comments

Comments
 (0)