Skip to content

Commit

Permalink
Remove last null from UDS address
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Thomasdezeeuw committed Jul 23, 2024
1 parent 91881b1 commit 5306ad3
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/sys/unix/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 5306ad3

Please sign in to comment.