File tree Expand file tree Collapse file tree 1 file changed +5
-6
lines changed Expand file tree Collapse file tree 1 file changed +5
-6
lines changed Original file line number Diff line number Diff line change 13
13
//! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2
14
14
//! which also explains why this crate should not use getentropy(2).
15
15
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 } ;
17
17
18
18
const MAX_BYTES : usize = 1024 ;
19
19
@@ -22,12 +22,11 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
22
22
let ptr = chunk. as_mut_ptr ( ) . cast :: < c_void > ( ) ;
23
23
let ret = unsafe { libc:: getrandom ( ptr, chunk. len ( ) , libc:: GRND_RANDOM ) } ;
24
24
// 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
- }
28
25
// 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 ) ,
31
30
}
32
31
}
33
32
Ok ( ( ) )
You can’t perform that action at this time.
0 commit comments