Skip to content

Commit 6c65a13

Browse files
committed
SocketAddr of Unix Sockets use sun_len as path's length in BSD-like OSes
- fixes #69061
1 parent 539d7bd commit 6c65a13

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,26 @@ impl SocketAddr {
209209
} else if self.addr.sun_path[0] == 0 {
210210
AddressKind::Abstract(&path[1..len])
211211
} else {
212-
AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
212+
cfg_if! {
213+
if #[cfg(any(target_os = "macos",
214+
target_os = "ios",
215+
target_os = "freebsd",
216+
target_os = "dragonfly",
217+
target_os = "openbsd",
218+
target_os = "netbsd"))] {
219+
// BSD-like systems may not include the last '\0' in length,
220+
// because they have a sun_len as the length of sun_path
221+
let sun_len = self.addr.sun_len;
222+
} else {
223+
// Trim the last '\0'
224+
let mut sun_len = len;
225+
while sun_len > 0 && path[sun_len] == 0 {
226+
sun_len -= 1;
227+
}
228+
}
229+
}
230+
231+
AddressKind::Pathname(OsStr::from_bytes(&path[..sun_len]).as_ref())
213232
}
214233
}
215234
}

0 commit comments

Comments
 (0)