From d9c611f91e0812fc7aacbab3ed3c77f4c0705d4e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 19 Apr 2019 14:43:37 +0200 Subject: [PATCH] fix unaligned and illegal-provenance reads in next_u64 --- rand_core/src/block.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index 3045b9482f4..6772a18a895 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -51,7 +51,7 @@ //! [`fill_bytes`]: RngCore::fill_bytes use core::convert::AsRef; -use core::fmt; +use core::{fmt, ptr}; use {RngCore, CryptoRng, SeedableRng, Error}; use impls::{fill_via_u32_chunks, fill_via_u64_chunks}; @@ -183,7 +183,8 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> let read_u64 = |results: &[u32], index| { if cfg!(any(target_arch = "x86", target_arch = "x86_64")) { // requires little-endian CPU supporting unaligned reads: - unsafe { *(&results[index] as *const u32 as *const u64) } + let ptr: *const u64 = results[index..index+1].as_ptr() as *const u64; + unsafe { ptr::read_unaligned(ptr) } } else { let x = u64::from(results[index]); let y = u64::from(results[index + 1]);