From b0c4a35a969aa94f6667cb885eebb184ee318a6d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 22 Nov 2018 08:57:26 +0100 Subject: VecDeque::drain: make sure the 'drain' raw pointer is actually still usable --- src/liballoc/collections/vec_deque.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/vec_deque.rs b/src/liballoc/collections/vec_deque.rs index c8ee40f3d27..60b5d8063bf 100644 --- a/src/liballoc/collections/vec_deque.rs +++ b/src/liballoc/collections/vec_deque.rs @@ -1019,14 +1019,19 @@ impl VecDeque { // the drain is complete and the Drain destructor is run. self.head = drain_tail; + // `deque` and `ring` overlap in what they point to, so we must make sure + // that `ring` is "derived-from" `deque`, or else even just creating ring + // from `self` already invalidates `deque`. + let deque = NonNull::from(&mut *self); + Drain { - deque: NonNull::from(&mut *self), + deque, after_tail: drain_head, after_head: head, iter: Iter { tail: drain_tail, head: drain_head, - ring: unsafe { self.buffer_as_mut_slice() }, + ring: unsafe { (&mut *deque.as_ptr()).buffer_as_mut_slice() }, }, } } -- cgit 1.4.1-3-g733a5