@@ -7,7 +7,7 @@ use core::{
7
7
cell:: UnsafeCell ,
8
8
ffi:: c_void,
9
9
mem:: MaybeUninit ,
10
- sync:: atomic:: { AtomicUsize , Ordering :: Relaxed } ,
10
+ sync:: atomic:: { AtomicI32 , Ordering :: Relaxed } ,
11
11
} ;
12
12
13
13
/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
@@ -17,7 +17,7 @@ use core::{
17
17
/// - On AIX, /dev/urandom will "provide cryptographically secure output".
18
18
/// - On Haiku and QNX Neutrino they are identical.
19
19
const FILE_PATH : & [ u8 ] = b"/dev/urandom\0 " ;
20
- const FD_UNINIT : usize = usize :: max_value ( ) ;
20
+ const FD_UNINIT : i32 = i32 :: MIN ;
21
21
22
22
pub fn getrandom_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
23
23
let fd = get_rng_fd ( ) ?;
@@ -30,11 +30,12 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
30
30
// bytes. The file will be opened exactly once. All subsequent calls will
31
31
// return the same file descriptor. This file descriptor is never closed.
32
32
fn get_rng_fd ( ) -> Result < libc:: c_int , Error > {
33
- static FD : AtomicUsize = AtomicUsize :: new ( FD_UNINIT ) ;
33
+ const _: ( ) = assert ! ( core:: mem:: size_of:: <libc:: c_int>( ) == core:: mem:: size_of:: <i32 >( ) ) ;
34
+ static FD : AtomicI32 = AtomicI32 :: new ( FD_UNINIT ) ;
34
35
fn get_fd ( ) -> Option < libc:: c_int > {
35
36
match FD . load ( Relaxed ) {
36
37
FD_UNINIT => None ,
37
- val => Some ( val as libc :: c_int ) ,
38
+ val => Some ( val) ,
38
39
}
39
40
}
40
41
@@ -59,8 +60,9 @@ fn get_rng_fd() -> Result<libc::c_int, Error> {
59
60
60
61
let fd = open_readonly ( FILE_PATH ) ?;
61
62
// The fd always fits in a usize without conflicting with FD_UNINIT.
62
- debug_assert ! ( fd >= 0 && ( fd as usize ) < FD_UNINIT ) ;
63
- FD . store ( fd as usize , Relaxed ) ;
63
+ debug_assert ! ( fd >= 0 ) ;
64
+ const _: ( ) = assert ! ( FD_UNINIT < 0 ) ;
65
+ FD . store ( fd, Relaxed ) ;
64
66
65
67
Ok ( fd)
66
68
}
0 commit comments