Skip to content

Commit 6e7bbda

Browse files
committed
std::rand: seed ISAAC with no transmutes.
Slice transmutes are now (and, really, always were) dangerous, so we avoid them and do the (only?) non-(undefined behaviour in C) pointer cast: casting to *u8.
1 parent b8932c6 commit 6e7bbda

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/libstd/rand/isaac.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
//! The ISAAC random number generator.
1212
13-
use cast;
1413
use rand::{Rng, SeedableRng, OSRng};
1514
use iter::{Iterator, range, range_step, Repeat};
1615
use option::{None, Some};
16+
use vec::raw;
17+
use mem;
1718

1819
static RAND_SIZE_LEN: u32 = 8;
1920
static RAND_SIZE: u32 = 1 << RAND_SIZE_LEN;
@@ -42,9 +43,12 @@ impl IsaacRng {
4243
pub fn new() -> IsaacRng {
4344
let mut rng = EMPTY;
4445

45-
{
46-
let bytes = unsafe {cast::transmute::<&mut [u32], &mut [u8]>(rng.rsl)};
47-
OSRng::new().fill_bytes(bytes);
46+
unsafe {
47+
let ptr = raw::to_mut_ptr(rng.rsl);
48+
49+
do raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl)) |slice| {
50+
OSRng::new().fill_bytes(slice);
51+
}
4852
}
4953

5054
rng.init(true);
@@ -238,10 +242,15 @@ impl Isaac64Rng {
238242
/// seed.
239243
pub fn new() -> Isaac64Rng {
240244
let mut rng = EMPTY_64;
241-
{
242-
let bytes = unsafe {cast::transmute::<&mut [u64], &mut [u8]>(rng.rsl)};
243-
OSRng::new().fill_bytes(bytes);
245+
246+
unsafe {
247+
let ptr = raw::to_mut_ptr(rng.rsl);
248+
249+
do raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl)) |slice| {
250+
OSRng::new().fill_bytes(slice);
251+
}
244252
}
253+
245254
rng.init(true);
246255
rng
247256
}

0 commit comments

Comments
 (0)