Delegate <SocketAddr as Debug> to ByteStr

This allows UTF-8 characters to be printed without escapes, rather than
just ASCII.
This commit is contained in:
Tamir Duberstein 2025-05-24 07:44:31 -04:00
parent 6c8138de8f
commit ded2afcb23
No known key found for this signature in database
2 changed files with 14 additions and 4 deletions

View file

@ -1,3 +1,4 @@
use crate::bstr::ByteStr;
use crate::ffi::OsStr;
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
use crate::os::net::linux_ext;
@ -61,7 +62,7 @@ pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::s
enum AddressKind<'a> {
Unnamed,
Pathname(&'a Path),
Abstract(&'a [u8]),
Abstract(&'a ByteStr),
}
/// An address associated with a Unix socket.
@ -245,7 +246,7 @@ impl SocketAddr {
{
AddressKind::Unnamed
} else if self.addr.sun_path[0] == 0 {
AddressKind::Abstract(&path[1..len])
AddressKind::Abstract(ByteStr::from_bytes(&path[1..len]))
} else {
AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
}
@ -260,7 +261,7 @@ impl Sealed for SocketAddr {}
#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
impl linux_ext::addr::SocketAddrExt for SocketAddr {
fn as_abstract_name(&self) -> Option<&[u8]> {
if let AddressKind::Abstract(name) = self.address() { Some(name) } else { None }
if let AddressKind::Abstract(name) = self.address() { Some(name.as_bytes()) } else { None }
}
fn from_abstract_name<N>(name: N) -> crate::io::Result<Self>
@ -295,7 +296,7 @@ impl fmt::Debug for SocketAddr {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.address() {
AddressKind::Unnamed => write!(fmt, "(unnamed)"),
AddressKind::Abstract(name) => write!(fmt, "\"{}\" (abstract)", name.escape_ascii()),
AddressKind::Abstract(name) => write!(fmt, "{name:?} (abstract)"),
AddressKind::Pathname(path) => write!(fmt, "{path:?} (pathname)"),
}
}

View file

@ -411,6 +411,15 @@ fn test_unix_datagram_timeout_zero_duration() {
assert_eq!(err.kind(), ErrorKind::InvalidInput);
}
#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
fn abstract_socket_addr_debug() {
assert_eq!(
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)"#,
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()),
);
}
#[test]
fn abstract_namespace_not_allowed_connect() {
assert!(UnixStream::connect("\0asdf").is_err());