Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a possible segfault in heim::net::nic() for Windows #336

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions heim-cpu/src/sys/linux/freq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ops::Add<CpuFrequency> for CpuFrequency {
(None, None) => None,
};

CpuFrequency { current, max, min }
CpuFrequency { current, min, max }
}
}

Expand All @@ -69,7 +69,7 @@ fn _frequencies() -> impl Iterator<Item = Result<CpuFrequency>> {
let max = max_freq(&path);
let min = min_freq(&path);

Ok(CpuFrequency { current, max, min })
Ok(CpuFrequency { current, min, max })
})
}

Expand Down
6 changes: 3 additions & 3 deletions heim-disk/src/sys/linux/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion heim-memory/src/sys/macos/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pub async fn memory() -> Result<Memory> {
Ok(Memory {
total,
available,
free,
used,
free,
active,
inactive,
wire,
Expand Down
2 changes: 1 addition & 1 deletion heim-memory/src/sys/macos/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub async fn swap() -> Result<Swap> {

Ok(Swap {
total,
free,
used,
free,
sin,
sout,
})
Expand Down
92 changes: 44 additions & 48 deletions heim-net/src/sys/windows/nic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,31 +190,28 @@ pub async fn nic() -> Result<impl Stream<Item = Result<Nic>> + 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;

loop {
let iface_index;
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;
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())
Expand All @@ -231,40 +228,39 @@ pub async fn nic() -> Result<impl Stream<Item = Result<Nic>> + Send + Sync> {
};

// Walk through every IP address of this interface
loop {
let mut p_next_address = cur_iface.FirstUnicastAddress;

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;
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))
Expand Down
5 changes: 1 addition & 4 deletions heim-process/src/sys/unix/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ impl<'e> Iterator for EnvironmentIter<'e> {
type Item = (&'e OsStr, &'e OsStr);

fn next(&mut self) -> Option<Self::Item> {
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<usize>) {
Expand Down