From 6f1e16c0a61c32d0c53159b4368c6b49acf82624 Mon Sep 17 00:00:00 2001 From: auronandace Date: Sat, 28 Feb 2026 17:19:29 +0000 Subject: [PATCH] sys_socket and termios header cleanup --- src/header/sys_random/mod.rs | 2 +- src/header/sys_socket/mod.rs | 25 ++++++++++++------------- src/header/termios/mod.rs | 12 ++++++------ 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/header/sys_random/mod.rs b/src/header/sys_random/mod.rs index 4d3f3f1198..57040d469a 100644 --- a/src/header/sys_random/mod.rs +++ b/src/header/sys_random/mod.rs @@ -21,7 +21,7 @@ pub const GRND_RANDOM: c_uint = 2; #[unsafe(no_mangle)] pub unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_uint) -> ssize_t { Sys::getrandom( - unsafe { slice::from_raw_parts_mut(buf as *mut u8, buflen as usize) }, + unsafe { slice::from_raw_parts_mut(buf.cast::(), buflen) }, flags, ) .map(|read| read as ssize_t) diff --git a/src/header/sys_socket/mod.rs b/src/header/sys_socket/mod.rs index a71b57ae5c..bc9b7542ad 100644 --- a/src/header/sys_socket/mod.rs +++ b/src/header/sys_socket/mod.rs @@ -86,7 +86,7 @@ const _SS_PADDING: usize = _SS_MAXSIZE - mem::size_of::() - mem::si /// * The underscore fields are implementation specific details for padding that may change /// * [`usize`] is used because it's the width of a pointer for a given platform /// * The order of the fields is important because the bytes in the padding will be cast to and -/// from protocol structs in C +/// from protocol structs in C #[repr(C)] //#[derive(CheckVsLibcCrate)] FIXME: can't ignore private fields yet pub struct sockaddr_storage { @@ -106,13 +106,13 @@ pub unsafe extern "C" fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar { } pub unsafe extern "C" fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar { - unsafe { ((*mhdr).msg_control as *mut c_uchar).offset((*mhdr).msg_controllen as isize) } + unsafe { ((*mhdr).msg_control.cast::()).add((*mhdr).msg_controllen) } } /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar { - unsafe { (cmsg as *mut c_uchar).offset(CMSG_ALIGN(mem::size_of::()) as isize) } + unsafe { (cmsg as *mut c_uchar).add(CMSG_ALIGN(mem::size_of::())) } } /// See . @@ -123,14 +123,13 @@ pub unsafe extern "C" fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) }; unsafe { - let next = cmsg as usize - + CMSG_ALIGN((*cmsg).cmsg_len as usize) - + CMSG_ALIGN(mem::size_of::()); - let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize; + let next = + cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len) + CMSG_ALIGN(mem::size_of::()); + let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen; if next > max { - 0 as *mut cmsghdr + ptr::null_mut::() } else { - (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr + (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len)) as *mut cmsghdr } } } @@ -139,10 +138,10 @@ pub unsafe extern "C" fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) #[unsafe(no_mangle)] pub unsafe extern "C" fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr { unsafe { - if (*mhdr).msg_controllen as usize >= mem::size_of::() { - (*mhdr).msg_control as *mut cmsghdr + if (*mhdr).msg_controllen >= mem::size_of::() { + (*mhdr).msg_control.cast::() } else { - 0 as *mut cmsghdr + ptr::null_mut::() } } } @@ -427,7 +426,7 @@ pub unsafe extern "C" fn socketpair( ) -> c_int { trace_expr!( Sys::socketpair(domain, kind, protocol, unsafe { - &mut *(sv as *mut [c_int; 2]) + &mut *sv.cast::<[c_int; 2]>() }) .map(|()| 0) .or_minus_one_errno(), diff --git a/src/header/termios/mod.rs b/src/header/termios/mod.rs index 1615d2190d..9309f7a3e0 100644 --- a/src/header/termios/mod.rs +++ b/src/header/termios/mod.rs @@ -71,13 +71,13 @@ pub struct termios { /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn tcgetattr(fd: c_int, out: *mut termios) -> c_int { - unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TCGETS, out as *mut c_void) } + unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TCGETS, out.cast::()) } } /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn tcsetattr(fd: c_int, act: c_int, value: *const termios) -> c_int { - if act < 0 || act > 2 { + if !(0..=2).contains(&act) { platform::ERRNO.set(errno::EINVAL); return -1; } @@ -89,7 +89,7 @@ pub unsafe extern "C" fn tcsetattr(fd: c_int, act: c_int, value: *const termios) #[unsafe(no_mangle)] pub unsafe extern "C" fn tcgetsid(fd: c_int) -> pid_t { let mut sid = 0; - if unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TIOCGSID, (&raw mut sid) as *mut c_void) } < 0 { + if unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TIOCGSID, (&raw mut sid).cast::()) } < 0 { return -1; } sid @@ -196,7 +196,7 @@ pub unsafe extern "C" fn tcflush(fd: c_int, queue: c_int) -> c_int { /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn tcdrain(fd: c_int) -> c_int { - unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TCSBRK, 1 as *mut _) } + unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TCSBRK, core::ptr::dangling_mut()) } } /// See . @@ -204,7 +204,7 @@ pub unsafe extern "C" fn tcdrain(fd: c_int) -> c_int { pub unsafe extern "C" fn tcsendbreak(fd: c_int, _dur: c_int) -> c_int { // non-zero duration is ignored by musl due to it being // implementation-defined. we do the same. - unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TCSBRK, 0 as *mut _) } + unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TCSBRK, core::ptr::null_mut()) } } /// See . @@ -216,7 +216,7 @@ pub unsafe extern "C" fn tcgetwinsize(fd: c_int, sws: *mut winsize) -> c_int { /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn tcsetwinsize(fd: c_int, sws: *const winsize) -> c_int { - unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TIOCSWINSZ, (sws as *mut winsize).cast()) } + unsafe { sys_ioctl::ioctl(fd, sys_ioctl::TIOCSWINSZ, sws.cast_mut().cast()) } } /// See .