Skip to content

Commit

Permalink
Rollup merge of rust-lang#86318 - zonyitoo:master, r=yaahc
Browse files Browse the repository at this point in the history
SocketAddr of Unix Sockets use sun_len as path's length in BSD-like OSes

- fixes rust-lang#69061
  • Loading branch information
JohnTitor authored Jun 21, 2021
2 parents 77569d9 + 6c65a13 commit 56e4950
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 56e4950

Please sign in to comment.