about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2021-05-26 13:31:00 +0900
committerGitHub <noreply@github.com>2021-05-26 13:31:00 +0900
commitb7b9ce3df8918fb50a52b8d091805ff2cbf88043 (patch)
treef9aece3beb74462dbb097663274ef2ce5e68202a
parentd75521a36e4ead5addfa26fafe4e587bf9e80d2c (diff)
parentd7341f3c4bfd814a778461a465365e78a15e4de7 (diff)
downloadrust-b7b9ce3df8918fb50a52b8d091805ff2cbf88043.tar.gz
rust-b7b9ce3df8918fb50a52b8d091805ff2cbf88043.zip
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`
-rw-r--r--library/core/src/slice/mod.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index 0923175414e..3bcea4e6d25 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -3096,7 +3096,11 @@ impl<T> [T] {
         // 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);
         }
     }