Skip to content

Commit fe953dc

Browse files
committed
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.
1 parent ccfc38f commit fe953dc

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

Diff for: src/libstd/sys/unix/rand.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,19 @@ mod imp {
350350

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

356-
fn getrandom(buf: &mut [u8]) -> isize {
357-
unsafe { mx_cprng_draw(buf.as_mut_ptr(), buf.len()) }
356+
fn getrandom(buf: &mut [u8]) -> Result<usize, i32> {
357+
unsafe {
358+
let mut actual = 0;
359+
let status = mx_cprng_draw(buf.as_mut_ptr(), buf.len(), &mut actual);
360+
if status == 0 {
361+
Ok(actual)
362+
} else {
363+
Err(status)
364+
}
365+
}
358366
}
359367

360368
pub struct OsRng {
@@ -381,12 +389,16 @@ mod imp {
381389
let mut buf = v;
382390
while !buf.is_empty() {
383391
let ret = getrandom(buf);
384-
if ret < 0 {
385-
panic!("kernel mx_cprng_draw call failed! (returned {}, buf.len() {})",
386-
ret, buf.len());
392+
match ret {
393+
Err(err) => {
394+
panic!("kernel mx_cprng_draw call failed! (returned {}, buf.len() {})",
395+
err, buf.len())
396+
}
397+
Ok(actual) => {
398+
let move_buf = buf;
399+
buf = &mut move_buf[(actual as usize)..];
400+
}
387401
}
388-
let move_buf = buf;
389-
buf = &mut move_buf[(ret as usize)..];
390402
}
391403
}
392404
}

0 commit comments

Comments
 (0)