From 65c47d928e3b1ebc1601b71c3d4beb0c6e0988ff Mon Sep 17 00:00:00 2001 From: Nils Jeisecke Date: Wed, 17 Apr 2024 09:22:29 +0200 Subject: [PATCH] windows: avoid heap allocation 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). --- src/target/windows.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/target/windows.rs b/src/target/windows.rs index 8eb7e8c..da7dfcd 100644 --- a/src/target/windows.rs +++ b/src/target/windows.rs @@ -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, @@ -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 { - let mask = unsafe { malloc(size_of::()) 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