Use unsafe blocks in sys/uio.h implementation

This commit is contained in:
sourceturner
2026-01-18 21:19:40 +01:00
parent 3bfd14fcab
commit 3ccfc170ad
+20 -17
View File
@@ -2,6 +2,9 @@
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_uio.h.html>.
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use alloc::vec::Vec;
use core::slice;
@@ -25,14 +28,14 @@ pub struct iovec {
impl iovec {
unsafe fn to_slice(&self) -> &mut [u8] {
slice::from_raw_parts_mut(self.iov_base as *mut u8, self.iov_len as usize)
unsafe { slice::from_raw_parts_mut(self.iov_base as *mut u8, self.iov_len as usize) }
}
}
unsafe fn gather(iovs: &[iovec]) -> Vec<u8> {
let mut vec = Vec::new();
for iov in iovs.iter() {
vec.extend_from_slice(iov.to_slice());
vec.extend_from_slice(unsafe { iov.to_slice() });
}
vec
}
@@ -40,7 +43,7 @@ unsafe fn gather(iovs: &[iovec]) -> Vec<u8> {
unsafe fn scatter(iovs: &[iovec], vec: Vec<u8>) {
let mut i = 0;
for iov in iovs.iter() {
let slice = iov.to_slice();
let slice = unsafe { iov.to_slice() };
slice.copy_from_slice(&vec[i..][..slice.len()]);
i += slice.len();
}
@@ -59,12 +62,12 @@ pub unsafe extern "C" fn preadv(
return -1;
}
let iovs = slice::from_raw_parts(iov, iovcnt as usize);
let mut vec = gather(iovs);
let iovs = unsafe { slice::from_raw_parts(iov, iovcnt as usize) };
let mut vec = unsafe { gather(iovs) };
let ret = unistd::pread(fd, vec.as_mut_ptr() as *mut c_void, vec.len(), offset);
let ret = unsafe { unistd::pread(fd, vec.as_mut_ptr() as *mut c_void, vec.len(), offset) };
scatter(iovs, vec);
unsafe { scatter(iovs, vec) };
ret
}
@@ -82,10 +85,10 @@ pub unsafe extern "C" fn pwritev(
return -1;
}
let iovs = slice::from_raw_parts(iov, iovcnt as usize);
let vec = gather(iovs);
let iovs = unsafe { slice::from_raw_parts(iov, iovcnt as usize) };
let vec = unsafe { gather(iovs) };
unistd::pwrite(fd, vec.as_ptr() as *const c_void, vec.len(), offset)
unsafe { unistd::pwrite(fd, vec.as_ptr() as *const c_void, vec.len(), offset) }
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/readv.html>.
@@ -96,12 +99,12 @@ pub unsafe extern "C" fn readv(fd: c_int, iov: *const iovec, iovcnt: c_int) -> s
return -1;
}
let iovs = slice::from_raw_parts(iov, iovcnt as usize);
let mut vec = gather(iovs);
let iovs = unsafe { slice::from_raw_parts(iov, iovcnt as usize) };
let mut vec = unsafe { gather(iovs) };
let ret = unistd::read(fd, vec.as_mut_ptr() as *mut c_void, vec.len());
let ret = unsafe { unistd::read(fd, vec.as_mut_ptr() as *mut c_void, vec.len()) };
scatter(iovs, vec);
unsafe { scatter(iovs, vec) };
ret
}
@@ -114,8 +117,8 @@ pub unsafe extern "C" fn writev(fd: c_int, iov: *const iovec, iovcnt: c_int) ->
return -1;
}
let iovs = slice::from_raw_parts(iov, iovcnt as usize);
let vec = gather(iovs);
let iovs = unsafe { slice::from_raw_parts(iov, iovcnt as usize) };
let vec = unsafe { gather(iovs) };
unistd::write(fd, vec.as_ptr() as *const c_void, vec.len())
unsafe { unistd::write(fd, vec.as_ptr() as *const c_void, vec.len()) }
}