Skip to content

Commit

Permalink
Pass pointers, not slices, to libc::ioctl (#2181)
Browse files Browse the repository at this point in the history
Slices are not FFI-safe.
&[u8] and &mut [u8] are *wide pointers*, which means that at the moment,
we're getting lucky because they're passed via the "ScalarPair" ABI, and
this means that passing a `&[u8]` results in passing two arguments,
`*const u8` and `usize` for pointer and length. This passes an extra
argument to ioctl, which happens to work because the extra vararg is
skipped. We're getting lucky right now, and we should explicitly pass
the pointer we meant to pass instead.

This is normally checked on `extern "C"` functions, but `ioctl` in
particular uses varargs which prevents the compiler check from
reporting.
  • Loading branch information
maurer authored Nov 8, 2023
1 parent 6868489 commit 0ff4621
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/sys/ioctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
//! pub unsafe fn spi_message(fd: c_int, data: &mut [spi_ioc_transfer]) -> Result<c_int> {
//! let res = libc::ioctl(fd,
//! request_code_write!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MESSAGE, data.len() * mem::size_of::<spi_ioc_transfer>()),
//! data);
//! data.as_ptr());
//! Errno::result(res)
//! }
//! # fn main() {}
Expand Down Expand Up @@ -712,7 +712,7 @@ macro_rules! ioctl_read_buf {
pub unsafe fn $name(fd: $crate::libc::c_int,
data: &mut [$ty])
-> $crate::Result<$crate::libc::c_int> {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data))
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
}
)
}
Expand Down Expand Up @@ -751,7 +751,7 @@ macro_rules! ioctl_write_buf {
pub unsafe fn $name(fd: $crate::libc::c_int,
data: &[$ty])
-> $crate::Result<$crate::libc::c_int> {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data))
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_ptr()))
}
)
}
Expand Down Expand Up @@ -780,7 +780,7 @@ macro_rules! ioctl_readwrite_buf {
pub unsafe fn $name(fd: $crate::libc::c_int,
data: &mut [$ty])
-> $crate::Result<$crate::libc::c_int> {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data))
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
}
)
}

0 comments on commit 0ff4621

Please sign in to comment.