Rollup merge of #85610 - SkiFire13:fix-copy-within-provenance, r=oli-obk

Fix pointer provenance in <[T]>::copy_within

Previously the `self.as_mut_ptr()` invalidated the pointer created by the first `self.as_ptr()`. This also triggered miri when run with `-Zmiri-track-raw-pointers`
This commit is contained in:
Yuki Okushi
2021-05-26 13:31:00 +09:00
committed by GitHub
+5 -1
View File
@@ -3096,7 +3096,11 @@ pub fn copy_within<R: RangeBounds<usize>>(&mut self, src: R, dest: usize)
// SAFETY: the conditions for `ptr::copy` have all been checked above,
// as have those for `ptr::add`.
unsafe {
ptr::copy(self.as_ptr().add(src_start), self.as_mut_ptr().add(dest), count);
// Derive both `src_ptr` and `dest_ptr` from the same loan
let ptr = self.as_mut_ptr();
let src_ptr = ptr.add(src_start);
let dest_ptr = ptr.add(dest);
ptr::copy(src_ptr, dest_ptr, count);
}
}