Skip to content

Commit 0638780

Browse files
authored
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
2 parents bd4f67c + 7aaad6b commit 0638780

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ impl SocketAddr {
107107
addr: libc::sockaddr_un,
108108
mut len: libc::socklen_t,
109109
) -> io::Result<SocketAddr> {
110+
if cfg!(target_os = "openbsd") {
111+
// on OpenBSD, getsockname(2) returns the actual size of the socket address,
112+
// and not the len of the content. Figure out the length for ourselves.
113+
// https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2
114+
let sun_path: &[u8] =
115+
unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) };
116+
len = core::slice::memchr::memchr(0, sun_path)
117+
.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);
118+
}
119+
110120
if len == 0 {
111121
// When there is a datagram from unnamed unix socket
112122
// linux returns zero bytes of address

0 commit comments

Comments
 (0)