Skip to content

Commit

Permalink
Merge pull request rust-lang#20 from Wind-River/rand
Browse files Browse the repository at this point in the history
use randSecure and randABytes
  • Loading branch information
BaoshanPang authored Sep 11, 2019
2 parents 414d104 + 5287885 commit 83e7976
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/libstd/sys/vxworks/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ pub fn hashmap_random_keys() -> (u64, u64) {
mod imp {
use libc;
use crate::io;

extern "C" {
fn randBytes (randBuf: *mut libc::c_uchar,
numOfBytes: libc::c_int) -> libc::c_int;
}
use core::sync::atomic::{AtomicBool, Ordering::Relaxed};

pub fn fill_bytes(v: &mut [u8]) {
static RNG_INIT: AtomicBool = AtomicBool::new(false);
while !RNG_INIT.load(Relaxed) {
let ret = unsafe { libc::randSecure() };
if ret < 0 {
panic!("couldn't generate random bytes: {}", io::Error::last_os_error());
} else if ret > 0 {
RNG_INIT.store(true, Relaxed);
break;
}
unsafe { libc::usleep(10) };
}
let ret = unsafe {
randBytes(v.as_mut_ptr() as *mut libc::c_uchar, v.len() as libc::c_int)
libc::randABytes(v.as_mut_ptr() as *mut libc::c_uchar, v.len() as libc::c_int)
};
if ret == -1 {
if ret < 0 {
panic!("couldn't generate random bytes: {}", io::Error::last_os_error());
}
}
Expand Down

0 comments on commit 83e7976

Please sign in to comment.