Skip to content

Commit

Permalink
SocketAddr of Unix Sockets use sun_len as path's length in BSD-like OSes
Browse files Browse the repository at this point in the history
- fixes #69061
  • Loading branch information
zonyitoo committed Jun 15, 2021
1 parent 539d7bd commit 6c65a13
Showing 1 changed file with 20 additions and 1 deletion.
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! {
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

0 comments on commit 6c65a13

Please sign in to comment.