Skip to content

Commit

Permalink
Solaris: Do not read from errno when libc did not indicate error (#448)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
briansmith authored Jun 2, 2024
1 parent 40e873d commit c1e0d31
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/solaris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
let ptr = chunk.as_mut_ptr().cast::<c_void>();
let ret = unsafe { libc::getrandom(ptr, chunk.len(), libc::GRND_RANDOM) };
// In case the man page has a typo, we also check for negative ret.
if ret <= 0 {
return Err(last_os_error());
}
// If getrandom(2) succeeds, it should have completely filled chunk.
if (ret as usize) != chunk.len() {
return Err(Error::UNEXPECTED);
match usize::try_from(ret) {
Ok(ret) if ret == chunk.len() => {} // Good. Keep going.
Ok(0) => return Err(last_os_error()), // The syscall failed.
_ => return Err(Error::UNEXPECTED),
}
}
Ok(())
Expand Down

0 comments on commit c1e0d31

Please sign in to comment.