From 84b0e9a4db976edef7505c0bcd9b52fe8a360f28 Mon Sep 17 00:00:00 2001 From: Florian Gilcher Date: Wed, 14 Oct 2026 08:43:38 +0000 Subject: [PATCH] Fix unix pathname length when the kernel excludes the null byte QNX's getsockname() and getpeername() report the length of a pathname address without the terminating null byte. --- src/sys/unix.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 8027d325..8a8940f0 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -937,7 +937,17 @@ impl SockAddr { /// Should not be called on unnamed addresses. fn path_len(&self, storage: &libc::sockaddr_un) -> usize { debug_assert!(!self.is_unnamed()); - self.len() as usize - offset_of_path(storage) - 1 + let len = self.len() as usize - offset_of_path(storage); + if storage.sun_path[0] == 0 { + // Abstract name: skip the initial null byte. + len - 1 + } else if storage.sun_path[len - 1] == 0 { + // Pathname whose length includes the terminating null byte (e.g. Linux). + len - 1 + } else { + // Pathname whose length does not include it (e.g. FreeBSD, NetBSD, QNX). + len + } } /// Get a u8 slice for the bytes of the pathname or abstract name.