@@ -23,11 +23,12 @@ fn init() -> NonNull<c_void> {
23
23
let res_ptr = match NonNull :: new ( raw_ptr) {
24
24
Some ( fptr) => {
25
25
let getrandom_fn = unsafe { mem:: transmute :: < NonNull < c_void > , GetRandomFn > ( fptr) } ;
26
+ let dangling_ptr = ptr:: NonNull :: dangling ( ) . as_ptr ( ) ;
26
27
// Check that `getrandom` syscall is supported by kernel
27
- let res = unsafe { getrandom_fn ( ptr :: NonNull :: dangling ( ) . as_ptr ( ) , 0 , 0 ) } ;
28
+ let res = unsafe { getrandom_fn ( dangling_ptr , 0 , 0 ) } ;
28
29
if cfg ! ( getrandom_test_linux_fallback) {
29
30
NOT_AVAILABLE
30
- } else if res < 0 {
31
+ } else if res. is_negative ( ) {
31
32
match util_libc:: last_os_error ( ) . raw_os_error ( ) {
32
33
Some ( libc:: ENOSYS ) => NOT_AVAILABLE , // No kernel support
33
34
// The fallback on EPERM is intentionally not done on Android since this workaround
@@ -48,6 +49,12 @@ fn init() -> NonNull<c_void> {
48
49
res_ptr
49
50
}
50
51
52
+ // prevent inlining of the fallback implementation
53
+ #[ inline( never) ]
54
+ fn use_file_fallback ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
55
+ use_file:: getrandom_inner ( dest)
56
+ }
57
+
51
58
pub fn getrandom_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
52
59
// Despite being only a single atomic variable, we still cannot always use
53
60
// Ordering::Relaxed, as we need to make sure a successful call to `init`
@@ -62,15 +69,9 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
62
69
} ;
63
70
64
71
if fptr == NOT_AVAILABLE {
65
- // prevent inlining of the fallback implementation
66
- #[ inline( never) ]
67
- fn inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
68
- use_file:: getrandom_inner ( dest)
69
- }
70
-
71
- inner ( dest)
72
+ use_file_fallback ( dest)
72
73
} else {
73
- // note: `transume` is currently the only way to get function pointer
74
+ // note: `transume` is currently the only way to convert pointer into function reference
74
75
let getrandom_fn = unsafe { mem:: transmute :: < NonNull < c_void > , GetRandomFn > ( fptr) } ;
75
76
util_libc:: sys_fill_exact ( dest, |buf| unsafe {
76
77
getrandom_fn ( buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) , 0 )
0 commit comments