Skip to content

Commit c1e0d31

Browse files
authored
Solaris: Do not read from errno when libc did not indicate error (#448)
errno is only guaranteed to be set correctly when the function's return value indicates that the function failed. Handle the case where an unexpected negative result is returned separately from the case where the function failed.
1 parent 40e873d commit c1e0d31

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/solaris.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2222
let ptr = chunk.as_mut_ptr().cast::<c_void>();
2323
let ret = unsafe { libc::getrandom(ptr, chunk.len(), libc::GRND_RANDOM) };
2424
// In case the man page has a typo, we also check for negative ret.
25-
if ret <= 0 {
26-
return Err(last_os_error());
27-
}
2825
// If getrandom(2) succeeds, it should have completely filled chunk.
29-
if (ret as usize) != chunk.len() {
30-
return Err(Error::UNEXPECTED);
26+
match usize::try_from(ret) {
27+
Ok(ret) if ret == chunk.len() => {} // Good. Keep going.
28+
Ok(0) => return Err(last_os_error()), // The syscall failed.
29+
_ => return Err(Error::UNEXPECTED),
3130
}
3231
}
3332
Ok(())

0 commit comments

Comments
 (0)