From d8097117e9ead06a8825c065c597337f38811fe0 Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Wed, 13 Mar 2024 16:07:42 +0100 Subject: [PATCH] clippy::pedantic --- src/platform/windows/socket_table.rs | 18 +++++++++--------- src/platform/windows/tcp_listener.rs | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/platform/windows/socket_table.rs b/src/platform/windows/socket_table.rs index 9aa5e16..7700f08 100644 --- a/src/platform/windows/socket_table.rs +++ b/src/platform/windows/socket_table.rs @@ -21,18 +21,18 @@ impl SocketTable for TcpTable { } fn get_rows_count(table: &[u8]) -> usize { - let table = unsafe { &*(table.as_ptr() as *const TcpTable) }; + let table = unsafe { &*(table.as_ptr().cast::()) }; table.rows_count as usize } fn get_tcp_listener(table: &[u8], index: usize) -> Option { - let table = unsafe { &*(table.as_ptr() as *const TcpTable) }; - let rows_ptr = &table.rows[0] as *const TcpRow; + let table = unsafe { &*(table.as_ptr().cast::()) }; + let rows_ptr = std::ptr::addr_of!(&table.rows[0]); let row = unsafe { &*rows_ptr.add(index) }; if row.state == LISTEN { Some(TcpListener::new( IpAddr::V4(Ipv4Addr::from(u32::from_be(row.local_addr))), - u16::from_be(row.local_port as u16), + u16::from_be(u16::try_from(row.local_port).ok()?), row.owning_pid, )) } else { @@ -47,18 +47,18 @@ impl SocketTable for Tcp6Table { } fn get_rows_count(table: &[u8]) -> usize { - let table = unsafe { &*(table.as_ptr() as *const Tcp6Table) }; + let table = unsafe { &*(table.as_ptr().cast::()) }; table.rows_count as usize } fn get_tcp_listener(table: &[u8], index: usize) -> Option { - let table = unsafe { &*(table.as_ptr() as *const Tcp6Table) }; - let rows_ptr = &table.rows[0] as *const Tcp6Row; + let table = unsafe { &*(table.as_ptr().cast::()) }; + let rows_ptr = std::ptr::addr_of!(&table.rows[0]); let row = unsafe { &*rows_ptr.add(index) }; if row.state == LISTEN { Some(TcpListener::new( IpAddr::V6(Ipv6Addr::from(row.local_addr)), - u16::from_be(row.local_port as u16), + u16::from_be(u16::try_from(row.local_port).ok()?), row.owning_pid, )) } else { @@ -85,7 +85,7 @@ fn get_tcp_table(address_family: c_ulong) -> crate::Result> { table = Vec::::with_capacity(table_size as usize); err_code = unsafe { GetExtendedTcpTable( - table.as_mut_ptr() as *mut c_void, + table.as_mut_ptr().cast::(), &mut table_size, FALSE, address_family, diff --git a/src/platform/windows/tcp_listener.rs b/src/platform/windows/tcp_listener.rs index 1805923..c820e52 100644 --- a/src/platform/windows/tcp_listener.rs +++ b/src/platform/windows/tcp_listener.rs @@ -51,7 +51,7 @@ impl TcpListener { let h = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0).ok()? }; let mut process = unsafe { zeroed::() }; - process.dwSize = size_of::() as u32; + process.dwSize = u32::try_from(size_of::()).ok()?; if unsafe { Process32First(h, &mut process) }.is_ok() { loop { @@ -71,9 +71,9 @@ impl TcpListener { let name = process.szExeFile; let len = name.iter().position(|&x| x == 0)?; - Some(String::from_utf8( + String::from_utf8( name[0..len].iter().map(|e| *e as u8).collect(), - ).ok()?) + ).ok() } pub(super) fn to_listener(&self) -> Option {