From 5306ad3b4d8fcde133b6a7ccbb80c62cf7c9f9a8 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Tue, 23 Jul 2024 10:49:38 +0200 Subject: [PATCH] Remove last null from UDS address Before passing it to SocketAddr::from_pathname. This was a problem for Tokio's test suite where uds_socket::listen_and_stream failed. We can't reproduce the problem with just Mio's type as Mio doesn't allow a UnixStream to bound to a local path before connecting it. --- src/sys/unix/uds/listener.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/sys/unix/uds/listener.rs b/src/sys/unix/uds/listener.rs index bab5099e9..5b4219a29 100644 --- a/src/sys/unix/uds/listener.rs +++ b/src/sys/unix/uds/listener.rs @@ -109,9 +109,13 @@ pub(crate) fn accept(listener: &net::UnixListener) -> io::Result<(UnixStream, So if 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]) - })))?; + // SAFETY: going from i8 to u8 is fine in this context. + let mut path = + unsafe { &*(&sockaddr.sun_path[..path_len] as *const [libc::c_char] as *const [u8]) }; + // Remove last null as `SocketAddr::from_pathname` doesn't accept it. + if let Some(0) = path.last() { + path = &path[..path.len() - 1]; + } + let address = SocketAddr::from_pathname(Path::new(OsStr::from_bytes(path)))?; Ok((socket, address)) }