Skip to content

Commit 0ff4621

Browse files
authored
Pass pointers, not slices, to libc::ioctl (#2181)
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.
1 parent 6868489 commit 0ff4621

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/sys/ioctl/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
//! pub unsafe fn spi_message(fd: c_int, data: &mut [spi_ioc_transfer]) -> Result<c_int> {
182182
//! let res = libc::ioctl(fd,
183183
//! request_code_write!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MESSAGE, data.len() * mem::size_of::<spi_ioc_transfer>()),
184-
//! data);
184+
//! data.as_ptr());
185185
//! Errno::result(res)
186186
//! }
187187
//! # fn main() {}
@@ -712,7 +712,7 @@ macro_rules! ioctl_read_buf {
712712
pub unsafe fn $name(fd: $crate::libc::c_int,
713713
data: &mut [$ty])
714714
-> $crate::Result<$crate::libc::c_int> {
715-
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))
715+
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()))
716716
}
717717
)
718718
}
@@ -751,7 +751,7 @@ macro_rules! ioctl_write_buf {
751751
pub unsafe fn $name(fd: $crate::libc::c_int,
752752
data: &[$ty])
753753
-> $crate::Result<$crate::libc::c_int> {
754-
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))
754+
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()))
755755
}
756756
)
757757
}
@@ -780,7 +780,7 @@ macro_rules! ioctl_readwrite_buf {
780780
pub unsafe fn $name(fd: $crate::libc::c_int,
781781
data: &mut [$ty])
782782
-> $crate::Result<$crate::libc::c_int> {
783-
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))
783+
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()))
784784
}
785785
)
786786
}

0 commit comments

Comments
 (0)