Skip to content

Commit

Permalink
Rollup merge of rust-lang#37589 - raphlinus:fuchsia_random, r=alexcri…
Browse files Browse the repository at this point in the history
…chton

std: Track change to cprng syscall signature (Fuchsia)

The mx_cprng_draw syscall has changed signature to separate the status
and size return values, rather than multiplexing them into a single
value with errors interpreted as a negative value. This patch tracks
that change.
  • Loading branch information
Jonathan Turner authored Nov 5, 2016
2 parents c5a8dd3 + fe953dc commit 8f0b3b1
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/libstd/sys/unix/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,19 @@ mod imp {

#[link(name = "magenta")]
extern {
fn mx_cprng_draw(buffer: *mut u8, len: usize) -> isize;
fn mx_cprng_draw(buffer: *mut u8, len: usize, actual: *mut usize) -> i32;
}

fn getrandom(buf: &mut [u8]) -> isize {
unsafe { mx_cprng_draw(buf.as_mut_ptr(), buf.len()) }
fn getrandom(buf: &mut [u8]) -> Result<usize, i32> {
unsafe {
let mut actual = 0;
let status = mx_cprng_draw(buf.as_mut_ptr(), buf.len(), &mut actual);
if status == 0 {
Ok(actual)
} else {
Err(status)
}
}
}

pub struct OsRng {
Expand All @@ -381,12 +389,16 @@ mod imp {
let mut buf = v;
while !buf.is_empty() {
let ret = getrandom(buf);
if ret < 0 {
panic!("kernel mx_cprng_draw call failed! (returned {}, buf.len() {})",
ret, buf.len());
match ret {
Err(err) => {
panic!("kernel mx_cprng_draw call failed! (returned {}, buf.len() {})",
err, buf.len())
}
Ok(actual) => {
let move_buf = buf;
buf = &mut move_buf[(actual as usize)..];
}
}
let move_buf = buf;
buf = &mut move_buf[(ret as usize)..];
}
}
}
Expand Down

0 comments on commit 8f0b3b1

Please sign in to comment.