Skip to content

Commit

Permalink
Merge pull request #780 from RalfJung/as-byte-slice-mut
Browse files Browse the repository at this point in the history
fix AsByteSliceMut using raw pointers with bad provenance
  • Loading branch information
dhardy committed Apr 20, 2019
2 parents a506efe + d9c611f commit 476cb6d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
5 changes: 3 additions & 2 deletions rand_core/src/block.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -183,7 +183,8 @@ where <R as BlockRngCore>::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]);
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Expand Up @@ -391,8 +391,7 @@ macro_rules! impl_as_byte_slice {
}
} else {
unsafe {
slice::from_raw_parts_mut(&mut self[0]
as *mut $t
slice::from_raw_parts_mut(self.as_mut_ptr()
as *mut u8,
self.len() * mem::size_of::<$t>()
)
Expand Down

0 comments on commit 476cb6d

Please sign in to comment.