Skip to content

Commit bcad3fb

Browse files
josephlrnewpavlov
authored andcommitted
macos: Cleanup SecRandom type (#28)
1 parent 9e64082 commit bcad3fb

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/macos.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,20 @@ use crate::Error;
1313
use std::io;
1414
use core::num::NonZeroU32;
1515

16-
enum SecRandom {}
17-
18-
/// Essentially a null pointer (type `SecRandomRef`)
19-
#[allow(non_upper_case_globals)]
20-
const kSecRandomDefault: *const SecRandom = 0 as *const SecRandom;
16+
// TODO: Make extern once extern_types feature is stabilized. See:
17+
// https://github.com/rust-lang/rust/issues/43467
18+
#[repr(C)]
19+
struct SecRandom([u8; 0]);
2120

2221
#[link(name = "Security", kind = "framework")]
2322
extern {
24-
fn SecRandomCopyBytes(
25-
rnd: *const SecRandom, count: libc::size_t, bytes: *mut u8,
26-
) -> libc::c_int;
23+
static kSecRandomDefault: *const SecRandom;
24+
25+
fn SecRandomCopyBytes(rnd: *const SecRandom, count: usize, bytes: *mut u8) -> libc::c_int;
2726
}
2827

2928
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
30-
let ret = unsafe {
31-
SecRandomCopyBytes(
32-
kSecRandomDefault,
33-
dest.len() as libc::size_t,
34-
dest.as_mut_ptr(),
35-
)
36-
};
29+
let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) };
3730
if ret == -1 {
3831
error!("SecRandomCopyBytes call failed");
3932
Err(io::Error::last_os_error().into())

0 commit comments

Comments
 (0)