1
1
//! Implementation for VxWorks
2
2
use crate :: { util_libc:: last_os_error, Error } ;
3
3
use core:: {
4
+ cmp:: Ordering :: { Equal , Greater , Less } ,
4
5
mem:: MaybeUninit ,
5
6
sync:: atomic:: { AtomicBool , Ordering :: Relaxed } ,
6
7
} ;
@@ -9,17 +10,19 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
9
10
static RNG_INIT : AtomicBool = AtomicBool :: new ( false ) ;
10
11
while !RNG_INIT . load ( Relaxed ) {
11
12
let ret = unsafe { libc:: randSecure ( ) } ;
12
- if ret < 0 {
13
- return Err ( Error :: VXWORKS_RAND_SECURE ) ;
14
- } else if ret > 0 {
15
- RNG_INIT . store ( true , Relaxed ) ;
16
- break ;
13
+ match ret. cmp ( & 0 ) {
14
+ Greater => {
15
+ RNG_INIT . store ( true , Relaxed ) ;
16
+ break ;
17
+ }
18
+ Equal => unsafe { libc:: usleep ( 10 ) ; } ,
19
+ Less => return Err ( Error :: VXWORKS_RAND_SECURE ) ,
17
20
}
18
- unsafe { libc:: usleep ( 10 ) } ;
19
21
}
20
22
21
23
// Prevent overflow of i32
22
- for chunk in dest. chunks_mut ( i32:: max_value ( ) as usize ) {
24
+ let chunk_size = usize:: try_from ( i32:: MAX ) . expect ( "VxWorks does not support 16-bit targets" ) ;
25
+ for chunk in dest. chunks_mut ( chunk_size) {
23
26
let ret = unsafe { libc:: randABytes ( chunk. as_mut_ptr ( ) . cast :: < u8 > ( ) , chunk. len ( ) as i32 ) } ;
24
27
if ret != 0 {
25
28
return Err ( last_os_error ( ) ) ;
0 commit comments