Skip to content

Commit

Permalink
clippy::pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Mar 13, 2024
1 parent 69fa29c commit d809711
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/platform/windows/socket_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TcpTable>()) };
table.rows_count as usize
}

fn get_tcp_listener(table: &[u8], index: usize) -> Option<TcpListener> {
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::<TcpTable>()) };
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 {
Expand All @@ -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::<Tcp6Table>()) };
table.rows_count as usize
}

fn get_tcp_listener(table: &[u8], index: usize) -> Option<TcpListener> {
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::<Tcp6Table>()) };
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 {
Expand All @@ -85,7 +85,7 @@ fn get_tcp_table(address_family: c_ulong) -> crate::Result<Vec<u8>> {
table = Vec::<u8>::with_capacity(table_size as usize);
err_code = unsafe {
GetExtendedTcpTable(
table.as_mut_ptr() as *mut c_void,
table.as_mut_ptr().cast::<c_void>(),
&mut table_size,
FALSE,
address_family,
Expand Down
6 changes: 3 additions & 3 deletions src/platform/windows/tcp_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl TcpListener {
let h = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0).ok()? };

let mut process = unsafe { zeroed::<PROCESSENTRY32>() };
process.dwSize = size_of::<PROCESSENTRY32>() as u32;
process.dwSize = u32::try_from(size_of::<PROCESSENTRY32>()).ok()?;

if unsafe { Process32First(h, &mut process) }.is_ok() {
loop {
Expand All @@ -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<Listener> {
Expand Down

0 comments on commit d809711

Please sign in to comment.