Skip to content

Commit 98077fe

Browse files
committed
Be more cautious about drain drops
This makes a greater effort to ensure that in all cases where either `DrainProducer` or `SliceDrain` will drop data, there are no references left pointing to that memory. It's not actually clear that this would be a problem anyway, as long as there's nothing accessing that zombie data, but it's not too much trouble to avoid it. Fixes rayon-rs#1029.
1 parent 7069695 commit 98077fe

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/array.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ impl<T: Send, const N: usize> IndexedParallelIterator for IntoIter<T, N> {
7878
unsafe {
7979
// Drain every item, and then the local array can just fall out of scope.
8080
let mut array = ManuallyDrop::new(self.array);
81-
callback.callback(DrainProducer::new(&mut *array))
81+
let producer = DrainProducer::new(array.as_mut_slice());
82+
callback.callback(producer)
8283
}
8384
}
8485
}

src/vec.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ impl<'data, T: 'data + Send> Producer for DrainProducer<'data, T> {
225225

226226
impl<'data, T: 'data + Send> Drop for DrainProducer<'data, T> {
227227
fn drop(&mut self) {
228-
// use `Drop for [T]`
229-
unsafe { ptr::drop_in_place(self.slice) };
228+
// extract the slice so we can use `Drop for [T]`
229+
let slice_ptr = mem::take(&mut self.slice).as_mut_ptr();
230+
unsafe { ptr::drop_in_place(slice_ptr) };
230231
}
231232
}
232233

@@ -276,7 +277,9 @@ impl<'data, T: 'data> iter::FusedIterator for SliceDrain<'data, T> {}
276277
impl<'data, T: 'data> Drop for SliceDrain<'data, T> {
277278
fn drop(&mut self) {
278279
// extract the iterator so we can use `Drop for [T]`
279-
let iter = mem::replace(&mut self.iter, [].iter_mut());
280-
unsafe { ptr::drop_in_place(iter.into_slice()) };
280+
let slice_ptr = mem::replace(&mut self.iter, [].iter_mut())
281+
.into_slice()
282+
.as_mut_ptr();
283+
unsafe { ptr::drop_in_place(slice_ptr) };
281284
}
282285
}

0 commit comments

Comments
 (0)