Rollup merge of #123779 - semarie:notgull-openbsd-socket, r=Mark-Simulacrum

OpenBSD fix long socket addresses

Original diff from ``@notgull`` in #118349, small changes from me.

on OpenBSD, getsockname(2) returns the actual size of the socket address, and  not the len of the content. Figure out the length for ourselves. see https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2

Fixes #116523
This commit is contained in:
Matthias Krüger 2024-04-14 09:01:57 +02:00 committed by GitHub
commit 0638780570
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -107,6 +107,16 @@ pub(super) fn from_parts(
addr: libc::sockaddr_un, addr: libc::sockaddr_un,
mut len: libc::socklen_t, mut len: libc::socklen_t,
) -> io::Result<SocketAddr> { ) -> io::Result<SocketAddr> {
if cfg!(target_os = "openbsd") {
// on OpenBSD, getsockname(2) returns the actual size of the socket address,
// and not the len of the content. Figure out the length for ourselves.
// https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2
let sun_path: &[u8] =
unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) };
len = core::slice::memchr::memchr(0, sun_path)
.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);
}
if len == 0 { if len == 0 {
// When there is a datagram from unnamed unix socket // When there is a datagram from unnamed unix socket
// linux returns zero bytes of address // linux returns zero bytes of address