Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
larseggert committed Jan 14, 2025
1 parent 0535ada commit 0102457
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
30 changes: 15 additions & 15 deletions src/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
};

#[cfg(apple)]
const ALIGN: usize = size_of::<libc::c_int>();
const ALIGN: usize = std::mem::size_of::<libc::c_int>();

#[cfg(bsd)]
// See https://github.com/freebsd/freebsd-src/blob/524a425d30fce3d5e47614db796046830b1f6a83/sys/net/route.h#L362-L371
Expand All @@ -68,9 +68,9 @@ asserted_const_with_type!(AF_LINK, AddressFamily, libc::AF_LINK, i32);
asserted_const_with_type!(RTM_VERSION, u8, bindings::RTM_VERSION, u32);
asserted_const_with_type!(RTM_GET, u8, bindings::RTM_GET, u32);

const_assert!(size_of::<sockaddr_in>() + ALIGN <= u8::MAX as usize);
const_assert!(size_of::<sockaddr_in6>() + ALIGN <= u8::MAX as usize);
const_assert!(size_of::<rt_msghdr>() <= u8::MAX as usize);
const_assert!(std::mem::size_of::<sockaddr_in>() + ALIGN <= u8::MAX as usize);
const_assert!(std::mem::size_of::<sockaddr_in6>() + ALIGN <= u8::MAX as usize);
const_assert!(std::mem::size_of::<rt_msghdr>() <= u8::MAX as usize);

struct IfAddrs(*mut ifaddrs);

Expand Down Expand Up @@ -183,8 +183,8 @@ union SockaddrStorage {

fn sockaddr_len(af: AddressFamily) -> Result<usize> {
let sa_len = match af {
AF_INET => size_of::<sockaddr_in>(),
AF_INET6 => size_of::<sockaddr_in6>(),
AF_INET => std::mem::size_of::<sockaddr_in>(),
AF_INET6 => std::mem::size_of::<sockaddr_in6>(),
_ => {
return Err(Error::new(
ErrorKind::InvalidInput,
Expand All @@ -203,7 +203,7 @@ impl From<IpAddr> for SockaddrStorage {
#[cfg(not(target_os = "solaris"))]
#[allow(clippy::cast_possible_truncation)]
// `sockaddr_in` len is <= u8::MAX per `const_assert!` above.
sin_len: size_of::<sockaddr_in>() as u8,
sin_len: std::mem::size_of::<sockaddr_in>() as u8,
sin_family: AF_INET,
sin_addr: in_addr {
s_addr: u32::from_ne_bytes(ip.octets()),
Expand All @@ -217,7 +217,7 @@ impl From<IpAddr> for SockaddrStorage {
#[cfg(not(target_os = "solaris"))]
#[allow(clippy::cast_possible_truncation)]
// `sockaddr_in6` len is <= u8::MAX per `const_assert!` above.
sin6_len: size_of::<sockaddr_in6>() as u8,
sin6_len: std::mem::size_of::<sockaddr_in6>() as u8,
sin6_family: AF_INET6,
sin6_addr: in6_addr {
s6_addr: ip.octets(),
Expand Down Expand Up @@ -250,7 +250,7 @@ impl RouteMessage {
rtm: rt_msghdr {
#[allow(clippy::cast_possible_truncation)]
// `rt_msghdr` len + `ALIGN` is <= u8::MAX per `const_assert!` above.
rtm_msglen: (size_of::<rt_msghdr>() + sa_len) as u16,
rtm_msglen: (std::mem::size_of::<rt_msghdr>() + sa_len) as u16,
rtm_version: RTM_VERSION,
rtm_type: RTM_GET,
rtm_seq: seq,
Expand All @@ -276,14 +276,14 @@ impl RouteMessage {

impl From<&RouteMessage> for &[u8] {
fn from(value: &RouteMessage) -> Self {
debug_assert!(value.len() >= size_of::<Self>());
debug_assert!(value.len() >= std::mem::size_of::<Self>());
unsafe { slice::from_raw_parts(ptr::from_ref(value).cast(), value.len()) }
}
}

impl From<&[u8]> for rt_msghdr {
fn from(value: &[u8]) -> Self {
debug_assert!(value.len() >= size_of::<Self>());
debug_assert!(value.len() >= std::mem::size_of::<Self>());
unsafe { ptr::read_unaligned(value.as_ptr().cast()) }
}
}
Expand All @@ -304,15 +304,15 @@ fn if_index_mtu(remote: IpAddr) -> Result<(u16, Option<usize>)> {
loop {
let mut buf = vec![
0u8;
size_of::<rt_msghdr>() +
std::mem::size_of::<rt_msghdr>() +
// There will never be `RTAX_MAX` sockaddrs attached, but it's a safe upper bound.
(RTAX_MAX as usize * size_of::<sockaddr_storage>())
(RTAX_MAX as usize * std::mem::size_of::<sockaddr_storage>())
];
let len = fd.read(&mut buf[..])?;
if len < size_of::<rt_msghdr>() {
if len < std::mem::size_of::<rt_msghdr>() {
return Err(default_err());
}
let (reply, mut sa) = buf.split_at(size_of::<rt_msghdr>());
let (reply, mut sa) = buf.split_at(std::mem::size_of::<rt_msghdr>());
let reply: rt_msghdr = reply.into();
if !(reply.rtm_version == query_version
&& reply.rtm_pid == pid
Expand Down
4 changes: 3 additions & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ impl Drop for MibTablePtr {
fn drop(&mut self) {
if !self.0.is_null() {
// Free the memory allocated by GetIpInterfaceTable.
unsafe { FreeMibTable(self.0.cast()) };
unsafe {
FreeMibTable(self.0.cast());
}
}
}
}
Expand Down

0 comments on commit 0102457

Please sign in to comment.