From 93ee2fdb414150626d32d9c24907ec8850a187d2 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Sat, 4 Apr 2020 04:09:13 +0200 Subject: [PATCH 1/2] Drop unsafe code from SeedableRng::seed_from_u64 --- rand_core/src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 281bedde745..3dc9a405eb5 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -314,11 +314,7 @@ pub trait SeedableRng: Sized { let xorshifted = (((state >> 18) ^ state) >> 27) as u32; let rot = (state >> 59) as u32; let x = xorshifted.rotate_right(rot).to_le(); - - unsafe { - let p = &x as *const u32 as *const u8; - copy_nonoverlapping(p, chunk.as_mut_ptr(), chunk.len()); - } + chunk.copy_from_slice(&x.to_ne_bytes()); } Self::from_seed(seed) From 5489851e73e9ef47e3ee16466f11e829589117b2 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Sun, 5 Apr 2020 14:50:55 +0200 Subject: [PATCH 2/2] Use .to_le_bytes() instead of .to_le().to_ne_bytes() --- rand_core/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 3dc9a405eb5..fc341751a11 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -41,7 +41,6 @@ use core::convert::AsMut; use core::default::Default; -use core::ptr::copy_nonoverlapping; #[cfg(all(feature = "alloc", not(feature = "std")))] extern crate alloc; #[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::boxed::Box; @@ -313,8 +312,8 @@ pub trait SeedableRng: Sized { // Use PCG output function with to_le to generate x: let xorshifted = (((state >> 18) ^ state) >> 27) as u32; let rot = (state >> 59) as u32; - let x = xorshifted.rotate_right(rot).to_le(); - chunk.copy_from_slice(&x.to_ne_bytes()); + let x = xorshifted.rotate_right(rot); + chunk.copy_from_slice(&x.to_le_bytes()); } Self::from_seed(seed)