Skip to content

Commit 8dda9f4

Browse files
committed
Solaris: Replace cast with TryFrom.
1 parent 4cb5ca6 commit 8dda9f4

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/solaris.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2
1414
//! which also explains why this crate should not use getentropy(2).
1515
use crate::{util_libc::last_os_error, Error};
16-
use core::{ffi::c_void, mem::MaybeUninit};
16+
use core::{ffi::c_void, mem::MaybeUninit, num::NonZeroUsize};
1717

1818
const MAX_BYTES: usize = 1024;
1919

@@ -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()),
29+
_ => return Err(Error::UNEXPECTED),
3130
}
3231
}
3332
Ok(())

0 commit comments

Comments
 (0)