Skip to content

SocketAddr of Unix Sockets use sun_len as path's length in BSD-like OSes #86318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion library/std/src/os/unix/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,26 @@ impl SocketAddr {
} else if self.addr.sun_path[0] == 0 {
AddressKind::Abstract(&path[1..len])
} else {
AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
cfg_if::cfg_if! {
if #[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"))] {
// BSD-like systems may not include the last '\0' in length,
// because they have a sun_len as the length of sun_path
let sun_len = self.addr.sun_len;
} else {
// Trim the last '\0'
let mut sun_len = len;
while sun_len > 0 && path[sun_len] == 0 {
sun_len -= 1;
}
}
}

AddressKind::Pathname(OsStr::from_bytes(&path[..sun_len]).as_ref())
}
}
}
Expand Down