Skip to content

Commit

Permalink
windows: avoid heap allocation
Browse files Browse the repository at this point in the history
The windows function works on a pointer of known size,
so no allocation is required. This also fixes the leak.

Also use the winapi type (which actually is u32).
  • Loading branch information
njeisecke committed Apr 17, 2024
1 parent 6fe97d3 commit 65c47d9
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/target/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::slice::from_raw_parts;

use libc::{free, malloc, wchar_t, wcslen};
use winapi::{
ctypes::c_ulong,
shared::{
ws2def::{AF_UNSPEC, SOCKADDR_IN},
ws2ipdef::SOCKADDR_IN6,
Expand Down Expand Up @@ -264,12 +265,11 @@ fn ipv4_addr_equal(sockaddr1: &*mut SOCKADDR_IN, sockaddr2: &*mut SOCKADDR_IN) -
/// An implementation of `GetIpAddrTable` to get all available network interfaces would be required
/// in order to support previous versions of Windows.
fn make_ipv4_netmask(unicast_address: &*mut IP_ADAPTER_UNICAST_ADDRESS_LH) -> Netmask<Ipv4Addr> {
let mask = unsafe { malloc(size_of::<u32>()) as *mut u32 };
let mut mask: c_ulong = 0;
let on_link_prefix_length = unsafe { (*(*unicast_address)).OnLinkPrefixLength };

unsafe { ConvertLengthToIpv4Mask(on_link_prefix_length as u32, mask) };

let mask = unsafe { *mask };
unsafe {
ConvertLengthToIpv4Mask(on_link_prefix_length as u32, &mut mask as *mut c_ulong);
}

if cfg!(target_endian = "little") {
// due to a difference on how bytes are arranged on a
Expand Down

0 comments on commit 65c47d9

Please sign in to comment.