Skip to content

Commit 6716ad0

Browse files
Aaron1011newpavlov
authored andcommitted
Explicitly specify types to arguments of 'libc::syscall' (#74)
The 'libc::syscall' function uses varargs - as a result, its arguments are completely untyped. THe user must ensure that it is called with the proper types for the targeted syscall - otherwise, the calling convention might cause arguments to be put into the wrong registers. This commit explicitly casts the arguments to 'libc::syscall' to the proper type for the 'getrandom' syscall. This ensures that the correct types for the target platform will always be used, instead of relying on the types used happening to match those required by the target platform.
1 parent ab44edf commit 6716ad0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/linux_android.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1515
static HAS_GETRANDOM: LazyBool = LazyBool::new();
1616
if HAS_GETRANDOM.unsync_init(is_getrandom_available) {
1717
sys_fill_exact(dest, |buf| unsafe {
18-
libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr(), buf.len(), 0) as libc::ssize_t
18+
getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
1919
})
2020
} else {
2121
use_file::getrandom_inner(dest)
2222
}
2323
}
2424

2525
fn is_getrandom_available() -> bool {
26-
let res = unsafe { libc::syscall(libc::SYS_getrandom, 0, 0, libc::GRND_NONBLOCK) };
26+
let res = unsafe { getrandom(core::ptr::null_mut(), 0, libc::GRND_NONBLOCK) };
2727
if res < 0 {
2828
match last_os_error().raw_os_error() {
2929
Some(libc::ENOSYS) => false, // No kernel support
@@ -34,3 +34,11 @@ fn is_getrandom_available() -> bool {
3434
true
3535
}
3636
}
37+
38+
unsafe fn getrandom(
39+
buf: *mut libc::c_void,
40+
buflen: libc::size_t,
41+
flags: libc::c_uint,
42+
) -> libc::ssize_t {
43+
libc::syscall(libc::SYS_getrandom, buf, buflen, flags) as libc::ssize_t
44+
}

0 commit comments

Comments
 (0)