Skip to content

Commit 42c840b

Browse files
committed
fix unaligned and illegal-provenance reads in next_u64
1 parent 90501dc commit 42c840b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

rand_core/src/block.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
//! [`fill_bytes`]: RngCore::fill_bytes
5252
5353
use core::convert::AsRef;
54-
use core::fmt;
54+
use core::{fmt, ptr};
5555
use {RngCore, CryptoRng, SeedableRng, Error};
5656
use impls::{fill_via_u32_chunks, fill_via_u64_chunks};
5757

@@ -183,7 +183,8 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
183183
let read_u64 = |results: &[u32], index| {
184184
if cfg!(any(target_arch = "x86", target_arch = "x86_64")) {
185185
// requires little-endian CPU supporting unaligned reads:
186-
unsafe { *(&results[index] as *const u32 as *const u64) }
186+
let ptr: *const u64 = &results[index..index+1] as *const [u32] as *const u32 as *const u64;
187+
unsafe { ptr::read_unaligned(ptr) }
187188
} else {
188189
let x = u64::from(results[index]);
189190
let y = u64::from(results[index + 1]);

0 commit comments

Comments
 (0)