From fc13559036db82540d282726d830fce0e85a9b7c Mon Sep 17 00:00:00 2001 From: daladim Date: Wed, 23 Jun 2021 11:51:06 +0200 Subject: [PATCH 1/4] [Windows NIC] More checks for null pointers --- heim-net/src/sys/windows/nic.rs | 101 +++++++++++++++++--------------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/heim-net/src/sys/windows/nic.rs b/heim-net/src/sys/windows/nic.rs index d76e113e..70dd6605 100644 --- a/heim-net/src/sys/windows/nic.rs +++ b/heim-net/src/sys/windows/nic.rs @@ -190,31 +190,38 @@ pub async fn nic() -> Result> + Send + Sync> { } // Step 3 - walk through the list and populate our interfaces - let mut cur_iface = unsafe { - let p = buffer.as_ptr() as PIP_ADAPTER_ADDRESSES; - if p.is_null() { - // Unable to list interfaces - let e = Error::from(std::io::Error::from_raw_os_error(res as _)) - .with_ffi("GetAdaptersAddresses"); - return Err(e); - } - *p - }; + let mut p_next_iface = buffer.as_ptr() as PIP_ADAPTER_ADDRESSES; + if p_next_iface.is_null() { + // Unable to list interfaces + let e = Error::from(std::io::Error::from_raw_os_error(res as _)) + .with_ffi("GetAdaptersAddresses"); + return Err(e); + } + let mut cur_iface; loop { - let iface_index; + if p_next_iface.is_null() { + break; + } + cur_iface = unsafe { *p_next_iface }; + + let iface_index = unsafe { cur_iface.u.s().IfIndex }; let iface_guid_cstr; let iface_fname_ucstr; - let is_up; - let mut cur_address; - unsafe { - iface_index = cur_iface.u.s().IfIndex; - iface_guid_cstr = CStr::from_ptr(cur_iface.AdapterName); - iface_fname_ucstr = UCStr::from_ptr_str(cur_iface.FriendlyName); - cur_address = *(cur_iface.FirstUnicastAddress); - is_up = cur_iface.OperStatus == IfOperStatusUp; + iface_guid_cstr = if !cur_iface.AdapterName.is_null() { + CStr::from_ptr(cur_iface.AdapterName) + } else { + CStr::from_bytes_with_nul(&[0]).unwrap() + }; + + iface_fname_ucstr = if !cur_iface.FriendlyName.is_null() { + UCStr::from_ptr_str(cur_iface.FriendlyName) + } else { + UCStr::from_slice_with_nul(&[0]).unwrap() + }; } + let is_up = cur_iface.OperStatus == IfOperStatusUp; let iface_guid = iface_guid_cstr .to_str() .map(|s| s.to_string()) @@ -231,40 +238,42 @@ pub async fn nic() -> Result> + Send + Sync> { }; // Walk through every IP address of this interface + let mut p_next_address = cur_iface.FirstUnicastAddress; + let mut cur_address; loop { + if p_next_address.is_null() { + break; + } + cur_address = unsafe { *p_next_address }; + let this_socket_address = cur_address.Address; let this_netmask_length = cur_address.OnLinkPrefixLength; - let this_sa_family = unsafe { (*this_socket_address.lpSockaddr).sa_family }; - - let (this_address, this_netmask) = match this_sa_family as i32 { - AF_INET => ( - sockaddr_to_ipv4(this_socket_address), - Some(ipv4_netmask_address_from(this_netmask_length)), - ), - AF_INET6 => ( - sockaddr_to_ipv6(this_socket_address), - Some(ipv6_netmask_address_from(this_netmask_length)), - ), - _ => (None, None), - }; - - let mut this_nic = base_nic.clone(); - this_nic.address = this_address; - this_nic.netmask = this_netmask; - results.push(Ok(this_nic)); - let next_address = cur_address.Next; - if next_address.is_null() { - break; + if !this_socket_address.lpSockaddr.is_null() { + let this_sa_family = unsafe { (*this_socket_address.lpSockaddr).sa_family }; + + let (this_address, this_netmask) = match this_sa_family as i32 { + AF_INET => ( + sockaddr_to_ipv4(this_socket_address), + Some(ipv4_netmask_address_from(this_netmask_length)), + ), + AF_INET6 => ( + sockaddr_to_ipv6(this_socket_address), + Some(ipv6_netmask_address_from(this_netmask_length)), + ), + _ => (None, None), + }; + + let mut this_nic = base_nic.clone(); + this_nic.address = this_address; + this_nic.netmask = this_netmask; + results.push(Ok(this_nic)); } - cur_address = unsafe { *next_address }; - } - let next_item = cur_iface.Next; - if next_item.is_null() { - break; + p_next_address = cur_address.Next; } - cur_iface = unsafe { *next_item }; + + p_next_iface = cur_iface.Next; } Ok(stream::iter(results)) From b65673200ebb7a26d43adc840070a9d6d6d1c22e Mon Sep 17 00:00:00 2001 From: daladim Date: Wed, 23 Jun 2021 16:30:51 +0200 Subject: [PATCH 2/4] [Windows NIC] Removed useless error handling A null pointer here means "no interface", not an error. An error is signalled by res != NO_ERROR --- heim-net/src/sys/windows/nic.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/heim-net/src/sys/windows/nic.rs b/heim-net/src/sys/windows/nic.rs index 70dd6605..daf5efc5 100644 --- a/heim-net/src/sys/windows/nic.rs +++ b/heim-net/src/sys/windows/nic.rs @@ -191,12 +191,6 @@ pub async fn nic() -> Result> + Send + Sync> { // Step 3 - walk through the list and populate our interfaces let mut p_next_iface = buffer.as_ptr() as PIP_ADAPTER_ADDRESSES; - if p_next_iface.is_null() { - // Unable to list interfaces - let e = Error::from(std::io::Error::from_raw_os_error(res as _)) - .with_ffi("GetAdaptersAddresses"); - return Err(e); - } let mut cur_iface; loop { From 44ee5e2d0f5ac6a818cddcee2dda8297c148b5e7 Mon Sep 17 00:00:00 2001 From: daladim Date: Wed, 23 Jun 2021 16:31:44 +0200 Subject: [PATCH 3/4] [minor] Simpler constructs --- heim-net/src/sys/windows/nic.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/heim-net/src/sys/windows/nic.rs b/heim-net/src/sys/windows/nic.rs index daf5efc5..9dc287ee 100644 --- a/heim-net/src/sys/windows/nic.rs +++ b/heim-net/src/sys/windows/nic.rs @@ -192,12 +192,8 @@ pub async fn nic() -> Result> + Send + Sync> { // Step 3 - walk through the list and populate our interfaces let mut p_next_iface = buffer.as_ptr() as PIP_ADAPTER_ADDRESSES; - let mut cur_iface; - loop { - if p_next_iface.is_null() { - break; - } - cur_iface = unsafe { *p_next_iface }; + while !p_next_iface.is_null() { + let cur_iface = unsafe { *p_next_iface }; let iface_index = unsafe { cur_iface.u.s().IfIndex }; let iface_guid_cstr; @@ -233,12 +229,9 @@ pub async fn nic() -> Result> + Send + Sync> { // Walk through every IP address of this interface let mut p_next_address = cur_iface.FirstUnicastAddress; - let mut cur_address; - loop { - if p_next_address.is_null() { - break; - } - cur_address = unsafe { *p_next_address }; + + while !p_next_address.is_null() { + let cur_address = unsafe { *p_next_address }; let this_socket_address = cur_address.Address; let this_netmask_length = cur_address.OnLinkPrefixLength; From 4adea5c566dfce51d55acd12a995f5c8d86c1fb4 Mon Sep 17 00:00:00 2001 From: daladim Date: Fri, 25 Jun 2021 11:17:17 +0200 Subject: [PATCH 4/4] [minor] Clippy --- heim-cpu/src/sys/linux/freq.rs | 4 ++-- heim-disk/src/sys/linux/counters.rs | 6 +++--- heim-memory/src/sys/macos/memory.rs | 2 +- heim-memory/src/sys/macos/swap.rs | 2 +- heim-process/src/sys/unix/env.rs | 5 +---- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/heim-cpu/src/sys/linux/freq.rs b/heim-cpu/src/sys/linux/freq.rs index 9242dee3..7a9ed565 100644 --- a/heim-cpu/src/sys/linux/freq.rs +++ b/heim-cpu/src/sys/linux/freq.rs @@ -46,7 +46,7 @@ impl ops::Add for CpuFrequency { (None, None) => None, }; - CpuFrequency { current, max, min } + CpuFrequency { current, min, max } } } @@ -69,7 +69,7 @@ fn _frequencies() -> impl Iterator> { let max = max_freq(&path); let min = min_freq(&path); - Ok(CpuFrequency { current, max, min }) + Ok(CpuFrequency { current, min, max }) }) } diff --git a/heim-disk/src/sys/linux/counters.rs b/heim-disk/src/sys/linux/counters.rs index 42c66bea..1277e9a9 100644 --- a/heim-disk/src/sys/linux/counters.rs +++ b/heim-disk/src/sys/linux/counters.rs @@ -104,12 +104,12 @@ impl FromStr for IoCounters { Ok(IoCounters { name, read_count, - read_merged_count, - read_bytes, write_count, - write_merged_count, + read_bytes, write_bytes, busy_time, + read_merged_count, + write_merged_count, }) } } diff --git a/heim-memory/src/sys/macos/memory.rs b/heim-memory/src/sys/macos/memory.rs index 0c4a1bf4..d5964ad7 100644 --- a/heim-memory/src/sys/macos/memory.rs +++ b/heim-memory/src/sys/macos/memory.rs @@ -68,8 +68,8 @@ pub async fn memory() -> Result { Ok(Memory { total, available, - free, used, + free, active, inactive, wire, diff --git a/heim-memory/src/sys/macos/swap.rs b/heim-memory/src/sys/macos/swap.rs index 1e8ec2a1..f95c4b44 100644 --- a/heim-memory/src/sys/macos/swap.rs +++ b/heim-memory/src/sys/macos/swap.rs @@ -48,8 +48,8 @@ pub async fn swap() -> Result { Ok(Swap { total, - free, used, + free, sin, sout, }) diff --git a/heim-process/src/sys/unix/env.rs b/heim-process/src/sys/unix/env.rs index b2464009..454a9ddd 100644 --- a/heim-process/src/sys/unix/env.rs +++ b/heim-process/src/sys/unix/env.rs @@ -93,10 +93,7 @@ impl<'e> Iterator for EnvironmentIter<'e> { type Item = (&'e OsStr, &'e OsStr); fn next(&mut self) -> Option { - match self.0.next() { - Some((k, v)) => Some((k.as_os_str(), v.as_os_str())), - None => None, - } + self.0.next().map(|(k, v)| (k.as_os_str(), v.as_os_str())) } fn size_hint(&self) -> (usize, Option) {