From 4f932a4660175b40705c8a99fcad3313796a373b Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Mon, 12 Feb 2024 12:47:30 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20zb,internal:=20Improve=20slice?= =?UTF-8?q?=20handling=20in=20win32=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on suggestion from @nerix: https://github.com/dbus2/zbus/pull/586#pullrequestreview-1874994014 --- zbus/src/win32.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/zbus/src/win32.rs b/zbus/src/win32.rs index f75e31e1b..c42abd0cf 100644 --- a/zbus/src/win32.rs +++ b/zbus/src/win32.rs @@ -212,9 +212,13 @@ pub fn socket_addr_get_pid(addr: &SocketAddr) -> Result { } let tcp_table = tcp_table.as_mut_ptr().cast::(); - let num_entries = unsafe { (*tcp_table).dwNumEntries }; - for i in 0..num_entries { - let entry = unsafe { *(*tcp_table).table.as_ptr().add(i as usize) }; + let entries = unsafe { + std::slice::from_raw_parts( + (*tcp_table).table.as_ptr(), + (*tcp_table).dwNumEntries as usize, + ) + }; + for entry in entries { let port = (entry.dwLocalPort & 0xFFFF) as u16; let port = u16::from_be(port);