Skip to content

Commit 02f5748

Browse files
authored
Unrolled build for #141493
Rollup merge of #141493 - tamird:addreskind-bytestr, r=joshtriplett Delegate `<SocketAddr as Debug>` to `ByteStr` This allows UTF-8 characters to be printed without escapes, rather than just ASCII. r? ``@joshtriplett``
2 parents 64033a4 + ded2afc commit 02f5748

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::bstr::ByteStr;
12
use crate::ffi::OsStr;
23
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
34
use crate::os::net::linux_ext;
@@ -61,7 +62,7 @@ pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::s
6162
enum AddressKind<'a> {
6263
Unnamed,
6364
Pathname(&'a Path),
64-
Abstract(&'a [u8]),
65+
Abstract(&'a ByteStr),
6566
}
6667

6768
/// An address associated with a Unix socket.
@@ -245,7 +246,7 @@ impl SocketAddr {
245246
{
246247
AddressKind::Unnamed
247248
} else if self.addr.sun_path[0] == 0 {
248-
AddressKind::Abstract(&path[1..len])
249+
AddressKind::Abstract(ByteStr::from_bytes(&path[1..len]))
249250
} else {
250251
AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
251252
}
@@ -260,7 +261,7 @@ impl Sealed for SocketAddr {}
260261
#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
261262
impl linux_ext::addr::SocketAddrExt for SocketAddr {
262263
fn as_abstract_name(&self) -> Option<&[u8]> {
263-
if let AddressKind::Abstract(name) = self.address() { Some(name) } else { None }
264+
if let AddressKind::Abstract(name) = self.address() { Some(name.as_bytes()) } else { None }
264265
}
265266

266267
fn from_abstract_name<N>(name: N) -> crate::io::Result<Self>
@@ -295,7 +296,7 @@ impl fmt::Debug for SocketAddr {
295296
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
296297
match self.address() {
297298
AddressKind::Unnamed => write!(fmt, "(unnamed)"),
298-
AddressKind::Abstract(name) => write!(fmt, "\"{}\" (abstract)", name.escape_ascii()),
299+
AddressKind::Abstract(name) => write!(fmt, "{name:?} (abstract)"),
299300
AddressKind::Pathname(path) => write!(fmt, "{path:?} (pathname)"),
300301
}
301302
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ fn test_unix_datagram_timeout_zero_duration() {
411411
assert_eq!(err.kind(), ErrorKind::InvalidInput);
412412
}
413413

414+
#[cfg(any(target_os = "android", target_os = "linux"))]
415+
#[test]
416+
fn abstract_socket_addr_debug() {
417+
assert_eq!(
418+
r#""\0hello world\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff" (abstract)"#,
419+
format!("{:?}", SocketAddr::from_abstract_name(b"\0hello world\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff").unwrap()),
420+
);
421+
}
422+
414423
#[test]
415424
fn abstract_namespace_not_allowed_connect() {
416425
assert!(UnixStream::connect("\0asdf").is_err());

0 commit comments

Comments
 (0)