File tree Expand file tree Collapse file tree 1 file changed +14
-7
lines changed Expand file tree Collapse file tree 1 file changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -14,17 +14,24 @@ pub fn hashmap_random_keys() -> (u64, u64) {
1414mod imp {
1515 use libc;
1616 use crate :: io;
17-
18- extern "C" {
19- fn randBytes ( randBuf : * mut libc:: c_uchar ,
20- numOfBytes : libc:: c_int ) -> libc:: c_int ;
21- }
17+ use core:: sync:: atomic:: { AtomicBool , Ordering :: Relaxed } ;
2218
2319 pub fn fill_bytes ( v : & mut [ u8 ] ) {
20+ static RNG_INIT : AtomicBool = AtomicBool :: new ( false ) ;
21+ while !RNG_INIT . load ( Relaxed ) {
22+ let ret = unsafe { libc:: randSecure ( ) } ;
23+ if ret < 0 {
24+ panic ! ( "couldn't generate random bytes: {}" , io:: Error :: last_os_error( ) ) ;
25+ } else if ret > 0 {
26+ RNG_INIT . store ( true , Relaxed ) ;
27+ break ;
28+ }
29+ unsafe { libc:: usleep ( 10 ) } ;
30+ }
2431 let ret = unsafe {
25- randBytes ( v. as_mut_ptr ( ) as * mut libc:: c_uchar , v. len ( ) as libc:: c_int )
32+ libc :: randABytes ( v. as_mut_ptr ( ) as * mut libc:: c_uchar , v. len ( ) as libc:: c_int )
2633 } ;
27- if ret == - 1 {
34+ if ret < 0 {
2835 panic ! ( "couldn't generate random bytes: {}" , io:: Error :: last_os_error( ) ) ;
2936 }
3037 }
You can’t perform that action at this time.
0 commit comments