Skip to content

Commit 781e3c8

Browse files
committed
OpenBSD fix long socket addresses
There is an OpenBSD bug where the "len" returned by functions like "getsockname" is too long and makes it so the resulting address contains zero bytes. While this isn't a problem for C code, where strings are null terminated anyways, it's a problem for Rust. This commit fixes this issue by adding a check that truncates the address length to the first zero when OpenBSD is detected. If there are no zeroes, it just uses the original length provided by the system. Signed-off-by: John Nunley <[email protected]>
1 parent 6eb9524 commit 781e3c8

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

library/std/src/os/unix/net/addr.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,17 @@ impl SocketAddr {
111111
// When there is a datagram from unnamed unix socket
112112
// linux returns zero bytes of address
113113
len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
114-
} else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
114+
} else if cfg!(target_os = "openbsd") {
115+
// OpenBSD has a bug where the socket name's length
116+
// is more than what the buffer actually contains.
117+
// Figure out the length for ourselves.
118+
// https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2
119+
let sun_path: &[u8] = unsafe { crate::mem::transmute::<&[i8], &[u8]>(&addr.sun_path) };
120+
len = crate::sys::memchr::memchr(0, sun_path)
121+
.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);
122+
}
123+
124+
if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
115125
return Err(io::const_io_error!(
116126
io::ErrorKind::InvalidInput,
117127
"file descriptor did not correspond to a Unix socket",

0 commit comments

Comments
 (0)