From e2000abcf23ed8a88e0f5c011c4864259e1fa1f8 Mon Sep 17 00:00:00 2001 From: Pointerbender Date: Tue, 25 May 2021 16:26:22 +0200 Subject: [PATCH] fixed UB in <[T]>::copy_within method --- library/core/src/slice/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 0923175414edd..28fa07829d1a4 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -3093,10 +3093,11 @@ impl [T] { let Range { start: src_start, end: src_end } = slice::range(src, ..self.len()); let count = src_end - src_start; assert!(dest <= self.len() - count, "dest is out of bounds"); + let ptr = self.as_mut_ptr(); // 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); + ptr::copy(ptr.add(src_start), ptr.add(dest), count); } }