diff --git a/Cargo.toml b/Cargo.toml index e7b20eb74..85acff949 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -667,9 +667,6 @@ should_panic_without_expect = "allow" # 2 doc_markdown = "allow" unused_self = "allow" enum_glob_use = "allow" -ptr_cast_constness = "allow" -borrow_as_ptr = "allow" -ptr_as_ptr = "allow" needless_raw_string_hashes = "allow" unreadable_literal = "allow" unnested_or_patterns = "allow" diff --git a/fuzz/uufuzz/src/lib.rs b/fuzz/uufuzz/src/lib.rs index 4a7b2ea72..e94ffd8b1 100644 --- a/fuzz/uufuzz/src/lib.rs +++ b/fuzz/uufuzz/src/lib.rs @@ -193,13 +193,8 @@ fn read_from_fd(fd: RawFd) -> String { let mut captured_output = Vec::new(); let mut read_buffer = [0; 1024]; loop { - let bytes_read = unsafe { - libc::read( - fd, - read_buffer.as_mut_ptr() as *mut libc::c_void, - read_buffer.len(), - ) - }; + let bytes_read = + unsafe { libc::read(fd, read_buffer.as_mut_ptr().cast(), read_buffer.len()) }; if bytes_read == -1 { eprintln!("Failed to read from the pipe"); diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 0ac59df17..6f6158850 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -439,7 +439,7 @@ fn enter_chroot(root: &Path, skip_chdir: bool) -> UResult<()> { .map_err(|e| ChrootError::CannotEnter("root".to_string(), e.into()))? .as_bytes_with_nul() .as_ptr() - .cast::(), + .cast(), ) }; diff --git a/src/uucore/src/lib/features/entries.rs b/src/uucore/src/lib/features/entries.rs index d3796890a..6a067e132 100644 --- a/src/uucore/src/lib/features/entries.rs +++ b/src/uucore/src/lib/features/entries.rs @@ -290,7 +290,7 @@ macro_rules! f { unsafe { let data = $fid(k); if !data.is_null() { - Ok($st::from_raw(ptr::read(data as *const _))) + Ok($st::from_raw(ptr::read(data.cast_const()))) } else { // FIXME: Resource limits, signals and I/O failure may // cause this too. See getpwnam(3). @@ -317,12 +317,12 @@ macro_rules! f { // f!(getgrnam, getgrgid, gid_t, Group); let data = $fnam(cstring.as_ptr()); if !data.is_null() { - return Ok($st::from_raw(ptr::read(data as *const _))); + return Ok($st::from_raw(ptr::read(data.cast_const()))); } if let Ok(id) = k.parse::<$t>() { let data = $fid(id); if !data.is_null() { - Ok($st::from_raw(ptr::read(data as *const _))) + Ok($st::from_raw(ptr::read(data.cast_const()))) } else { Err(IOError::new( ErrorKind::NotFound, diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 78dfcceb2..8051b2f43 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -347,19 +347,19 @@ impl From for MountInfo { fn from(statfs: StatFs) -> Self { let dev_name = unsafe { // spell-checker:disable-next-line - CStr::from_ptr(&statfs.f_mntfromname[0]) + CStr::from_ptr(statfs.f_mntfromname.as_ptr()) .to_string_lossy() .into_owned() }; let fs_type = unsafe { // spell-checker:disable-next-line - CStr::from_ptr(&statfs.f_fstypename[0]) + CStr::from_ptr(statfs.f_fstypename.as_ptr()) .to_string_lossy() .into_owned() }; let mount_dir_bytes = unsafe { // spell-checker:disable-next-line - CStr::from_ptr(&statfs.f_mntonname[0]).to_bytes() + CStr::from_ptr(statfs.f_mntonname.as_ptr()).to_bytes() }; let mount_dir = os_str_from_bytes(mount_dir_bytes).unwrap().into_owned(); @@ -506,7 +506,7 @@ pub fn read_fs_list() -> UResult> { ))] { let mut mount_buffer_ptr: *mut StatFs = ptr::null_mut(); - let len = unsafe { get_mount_info(&mut mount_buffer_ptr, 1_i32) }; + let len = unsafe { get_mount_info(&raw mut mount_buffer_ptr, 1_i32) }; if len < 0 { return Err(USimpleError::new(1, "get_mount_info() failed")); } @@ -668,10 +668,10 @@ impl FsUsage { let path = to_nul_terminated_wide_string(path); GetDiskFreeSpaceW( path.as_ptr(), - &mut sectors_per_cluster, - &mut bytes_per_sector, - &mut number_of_free_clusters, - &mut total_number_of_clusters, + &raw mut sectors_per_cluster, + &raw mut bytes_per_sector, + &raw mut number_of_free_clusters, + &raw mut total_number_of_clusters, ); } @@ -881,7 +881,7 @@ impl FsMeta for StatFs { fn fsid(&self) -> u64 { // Use type inference to determine the type of f_fsid // (libc::__fsid_t on Android, libc::fsid_t on other platforms) - let f_fsid: &[u32; 2] = unsafe { &*(&raw const self.f_fsid as *const [u32; 2]) }; + let f_fsid: &[u32; 2] = unsafe { &*(&raw const self.f_fsid).cast() }; ((u64::from(f_fsid[0])) << 32) | u64::from(f_fsid[1]) } #[cfg(not(any( @@ -932,7 +932,7 @@ pub fn statfs(path: &OsStr) -> Result { Ok(p) => { let mut buffer: StatFs = unsafe { mem::zeroed() }; unsafe { - match statfs_fn(p.as_ptr(), &mut buffer) { + match statfs_fn(p.as_ptr(), &raw mut buffer) { 0 => Ok(buffer), _ => { let errno = IOError::last_os_error().raw_os_error().unwrap_or(0); diff --git a/src/uucore/src/lib/features/systemd_logind.rs b/src/uucore/src/lib/features/systemd_logind.rs index 0e599cfe5..d34e8cc17 100644 --- a/src/uucore/src/lib/features/systemd_logind.rs +++ b/src/uucore/src/lib/features/systemd_logind.rs @@ -53,7 +53,7 @@ mod login { pub fn get_sessions() -> Result, Box> { let mut sessions_ptr: *mut *mut libc::c_char = ptr::null_mut(); - let result = unsafe { ffi::sd_get_sessions(&mut sessions_ptr) }; + let result = unsafe { ffi::sd_get_sessions(&raw mut sessions_ptr) }; if result < 0 { return Err(format!("sd_get_sessions failed: {result}").into()); @@ -71,11 +71,11 @@ mod login { let session_cstr = unsafe { CStr::from_ptr(session_ptr) }; sessions.push(session_cstr.to_string_lossy().into_owned()); - unsafe { libc::free(session_ptr as *mut libc::c_void) }; + unsafe { libc::free(session_ptr.cast()) }; i += 1; } - unsafe { libc::free(sessions_ptr as *mut libc::c_void) }; + unsafe { libc::free(sessions_ptr.cast()) }; } Ok(sessions) @@ -86,7 +86,7 @@ mod login { let session_cstring = CString::new(session_id)?; let mut uid: std::os::raw::c_uint = 0; - let result = unsafe { ffi::sd_session_get_uid(session_cstring.as_ptr(), &mut uid) }; + let result = unsafe { ffi::sd_session_get_uid(session_cstring.as_ptr(), &raw mut uid) }; if result < 0 { return Err( @@ -102,7 +102,8 @@ mod login { let session_cstring = CString::new(session_id)?; let mut usec: u64 = 0; - let result = unsafe { ffi::sd_session_get_start_time(session_cstring.as_ptr(), &mut usec) }; + let result = + unsafe { ffi::sd_session_get_start_time(session_cstring.as_ptr(), &raw mut usec) }; if result < 0 { return Err(format!( @@ -119,7 +120,7 @@ mod login { let session_cstring = CString::new(session_id)?; let mut tty_ptr: *mut libc::c_char = ptr::null_mut(); - let result = unsafe { ffi::sd_session_get_tty(session_cstring.as_ptr(), &mut tty_ptr) }; + let result = unsafe { ffi::sd_session_get_tty(session_cstring.as_ptr(), &raw mut tty_ptr) }; if result < 0 { return Err( @@ -134,7 +135,7 @@ mod login { let tty_cstr = unsafe { CStr::from_ptr(tty_ptr) }; let tty_string = tty_cstr.to_string_lossy().into_owned(); - unsafe { libc::free(tty_ptr as *mut libc::c_void) }; + unsafe { libc::free(tty_ptr.cast()) }; Ok(Some(tty_string)) } @@ -147,7 +148,7 @@ mod login { let mut host_ptr: *mut libc::c_char = ptr::null_mut(); let result = - unsafe { ffi::sd_session_get_remote_host(session_cstring.as_ptr(), &mut host_ptr) }; + unsafe { ffi::sd_session_get_remote_host(session_cstring.as_ptr(), &raw mut host_ptr) }; if result < 0 { return Err(format!( @@ -163,7 +164,7 @@ mod login { let host_cstr = unsafe { CStr::from_ptr(host_ptr) }; let host_string = host_cstr.to_string_lossy().into_owned(); - unsafe { libc::free(host_ptr as *mut libc::c_void) }; + unsafe { libc::free(host_ptr.cast()) }; Ok(Some(host_string)) } @@ -176,7 +177,7 @@ mod login { let mut display_ptr: *mut libc::c_char = ptr::null_mut(); let result = - unsafe { ffi::sd_session_get_display(session_cstring.as_ptr(), &mut display_ptr) }; + unsafe { ffi::sd_session_get_display(session_cstring.as_ptr(), &raw mut display_ptr) }; if result < 0 { return Err(format!( @@ -192,7 +193,7 @@ mod login { let display_cstr = unsafe { CStr::from_ptr(display_ptr) }; let display_string = display_cstr.to_string_lossy().into_owned(); - unsafe { libc::free(display_ptr as *mut libc::c_void) }; + unsafe { libc::free(display_ptr.cast()) }; Ok(Some(display_string)) } @@ -204,7 +205,8 @@ mod login { let session_cstring = CString::new(session_id)?; let mut type_ptr: *mut libc::c_char = ptr::null_mut(); - let result = unsafe { ffi::sd_session_get_type(session_cstring.as_ptr(), &mut type_ptr) }; + let result = + unsafe { ffi::sd_session_get_type(session_cstring.as_ptr(), &raw mut type_ptr) }; if result < 0 { return Err( @@ -219,7 +221,7 @@ mod login { let type_cstr = unsafe { CStr::from_ptr(type_ptr) }; let type_string = type_cstr.to_string_lossy().into_owned(); - unsafe { libc::free(type_ptr as *mut libc::c_void) }; + unsafe { libc::free(type_ptr.cast()) }; Ok(Some(type_string)) } @@ -231,7 +233,8 @@ mod login { let session_cstring = CString::new(session_id)?; let mut seat_ptr: *mut libc::c_char = ptr::null_mut(); - let result = unsafe { ffi::sd_session_get_seat(session_cstring.as_ptr(), &mut seat_ptr) }; + let result = + unsafe { ffi::sd_session_get_seat(session_cstring.as_ptr(), &raw mut seat_ptr) }; if result < 0 { return Err( @@ -246,7 +249,7 @@ mod login { let seat_cstr = unsafe { CStr::from_ptr(seat_ptr) }; let seat_string = seat_cstr.to_string_lossy().into_owned(); - unsafe { libc::free(seat_ptr as *mut libc::c_void) }; + unsafe { libc::free(seat_ptr.cast()) }; Ok(Some(seat_string)) } @@ -373,9 +376,9 @@ pub fn read_login_records() -> UResult> { let ret = libc::getpwuid_r( uid, passwd.as_mut_ptr(), - buf.as_mut_ptr() as *mut libc::c_char, + buf.as_mut_ptr().cast(), buf.len(), - &mut result, + &raw mut result, ); if ret == 0 && !result.is_null() { diff --git a/src/uucore/src/lib/features/uptime.rs b/src/uucore/src/lib/features/uptime.rs index c278ff21f..e29e2d17c 100644 --- a/src/uucore/src/lib/features/uptime.rs +++ b/src/uucore/src/lib/features/uptime.rs @@ -62,10 +62,9 @@ pub fn get_uptime(_boot_time: Option) -> UResult { tv_sec: 0, tv_nsec: 0, }; - let raw_tp = &mut tp as *mut timespec; // OpenBSD prototype: clock_gettime(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int; - let ret: c_int = unsafe { clock_gettime(CLOCK_BOOTTIME, raw_tp) }; + let ret: c_int = unsafe { clock_gettime(CLOCK_BOOTTIME, &raw mut tp) }; if ret == 0 { #[cfg(target_pointer_width = "64")] diff --git a/src/uucore/src/lib/features/utmpx.rs b/src/uucore/src/lib/features/utmpx.rs index 3c18cc16f..8832caff3 100644 --- a/src/uucore/src/lib/features/utmpx.rs +++ b/src/uucore/src/lib/features/utmpx.rs @@ -525,7 +525,7 @@ impl Iterator for UtmpxIter { // All the strings live inline in the struct as arrays, which // makes things easier. Some(UtmpxRecord::Traditional(Box::new(Utmpx { - inner: ptr::read(res as *const _), + inner: ptr::read(res.cast_const()), }))) } } diff --git a/tests/uutests/src/lib/util.rs b/tests/uutests/src/lib/util.rs index 108a2b056..5c5ed3ef4 100644 --- a/tests/uutests/src/lib/util.rs +++ b/tests/uutests/src/lib/util.rs @@ -1147,7 +1147,7 @@ impl AtPath { unsafe { let name = CString::new(self.plus_as_string(fifo)).unwrap(); let mut stat: libc::stat = std::mem::zeroed(); - if libc::stat(name.as_ptr(), &mut stat) >= 0 { + if libc::stat(name.as_ptr(), &raw mut stat) >= 0 { libc::S_IFIFO & stat.st_mode as libc::mode_t != 0 } else { false @@ -1160,7 +1160,7 @@ impl AtPath { unsafe { let name = CString::new(self.plus_as_string(char_dev)).unwrap(); let mut stat: libc::stat = std::mem::zeroed(); - if libc::stat(name.as_ptr(), &mut stat) >= 0 { + if libc::stat(name.as_ptr(), &raw mut stat) >= 0 { libc::S_IFCHR & stat.st_mode as libc::mode_t != 0 } else { false