diff --git a/library/std/src/os/unix/net/addr.rs b/library/std/src/os/unix/net/addr.rs index 6c99e8c36203a..5de9412c1aa49 100644 --- a/library/std/src/os/unix/net/addr.rs +++ b/library/std/src/os/unix/net/addr.rs @@ -111,7 +111,18 @@ impl SocketAddr { // When there is a datagram from unnamed unix socket // linux returns zero bytes of address len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address - } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t { + } else if cfg!(target_os = "openbsd") { + // OpenBSD implements getsockname in a way where the + // socket name's length is more than what the buffer + // actually contains. Figure out the length for ourselves. + // https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2 + let sun_path: &[u8] = + unsafe { crate::mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) }; + len = crate::sys::memchr::memchr(0, sun_path) + .map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t); + } + + if len == 0 && addr.sun_family != libc::AF_UNIX as libc::sa_family_t { return Err(io::const_io_error!( io::ErrorKind::InvalidInput, "file descriptor did not correspond to a Unix socket",