From 71b80e36e7c039fee136aeb3adf95a2c3784dad2 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Sun, 24 Dec 2023 19:22:53 +0100 Subject: [PATCH] Work around Darwn (macOS) weirdness It seems to return a length of 16 and an all zero address for unnamed Unix addresses. --- src/sys/unix/uds/listener.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/sys/unix/uds/listener.rs b/src/sys/unix/uds/listener.rs index ad036eef5..8c06c1b4a 100644 --- a/src/sys/unix/uds/listener.rs +++ b/src/sys/unix/uds/listener.rs @@ -96,7 +96,20 @@ pub(crate) fn accept(listener: &net::UnixListener) -> io::Result<(UnixStream, So }); let socket = socket.map(UnixStream::from_std)?; - let path_len = socklen as usize - path_offset(&sockaddr); + + #[allow(unused_mut)] // See below. + let mut path_len = socklen as usize - path_offset(&sockaddr); + // Darwin is being weird, it return a length of 16, but other an unnamed + // (all zero) address. Map that to a length of 0 to match other OS. + #[cfg(any( + target_os = "ios", + target_os = "macos", + target_os = "tvos", + target_os = "watchos", + ))] + if socklen == 16 && sockaddr.sun_path[0] == 0 { + path_len = 0; + } let address = SocketAddr::from_pathname(Path::new(OsStr::from_bytes(unsafe { // SAFETY: going from i8 to u8 is fine in this context. &*(&sockaddr.sun_path[..path_len] as *const [libc::c_char] as *const [u8])