Skip to content

Commit

Permalink
Use sockaddr_un in unix SocketAddr::from_path
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw committed Jan 27, 2022
1 parent ca9a3c9 commit 4acb8ac
Showing 1 changed file with 1 addition and 35 deletions.
36 changes: 1 addition & 35 deletions library/std/src/os/unix/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,41 +164,7 @@ impl SocketAddr {
where
P: AsRef<Path>,
{
// SAFETY: All zeros is a valid representation for `sockaddr_un`.
let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };

let bytes = path.as_ref().as_os_str().as_bytes();
if bytes.contains(&b'\0') {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"path can't contain null bytes",
));
} else if bytes.len() >= storage.sun_path.len() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"path must be shorter than SUN_LEN",
));
}

storage.sun_family = libc::AF_UNIX as _;
// SAFETY: `bytes` and `addr.sun_path` are not overlapping and
// both point to valid memory.
// NOTE: We zeroed the memory above, so the path is already null
// terminated.
unsafe {
ptr::copy_nonoverlapping(
bytes.as_ptr(),
storage.sun_path.as_mut_ptr().cast(),
bytes.len(),
)
};

let base = &storage as *const _ as usize;
let path = &storage.sun_path as *const _ as usize;
let sun_path_offset = path - base;
let length = sun_path_offset + bytes.len() + 1;

Ok(SocketAddr { addr: storage, len: length as _ })
sockaddr_un(path.as_ref()).map(|(addr, len)| SocketAddr { addr, len })
}

/// Returns `true` if the address is unnamed.
Expand Down

0 comments on commit 4acb8ac

Please sign in to comment.