Skip to content

Commit 4acb8ac

Browse files
committed
Use sockaddr_un in unix SocketAddr::from_path
1 parent ca9a3c9 commit 4acb8ac

File tree

1 file changed

+1
-35
lines changed

1 file changed

+1
-35
lines changed

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

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -164,41 +164,7 @@ impl SocketAddr {
164164
where
165165
P: AsRef<Path>,
166166
{
167-
// SAFETY: All zeros is a valid representation for `sockaddr_un`.
168-
let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
169-
170-
let bytes = path.as_ref().as_os_str().as_bytes();
171-
if bytes.contains(&b'\0') {
172-
return Err(io::Error::new(
173-
io::ErrorKind::InvalidInput,
174-
"path can't contain null bytes",
175-
));
176-
} else if bytes.len() >= storage.sun_path.len() {
177-
return Err(io::Error::new(
178-
io::ErrorKind::InvalidInput,
179-
"path must be shorter than SUN_LEN",
180-
));
181-
}
182-
183-
storage.sun_family = libc::AF_UNIX as _;
184-
// SAFETY: `bytes` and `addr.sun_path` are not overlapping and
185-
// both point to valid memory.
186-
// NOTE: We zeroed the memory above, so the path is already null
187-
// terminated.
188-
unsafe {
189-
ptr::copy_nonoverlapping(
190-
bytes.as_ptr(),
191-
storage.sun_path.as_mut_ptr().cast(),
192-
bytes.len(),
193-
)
194-
};
195-
196-
let base = &storage as *const _ as usize;
197-
let path = &storage.sun_path as *const _ as usize;
198-
let sun_path_offset = path - base;
199-
let length = sun_path_offset + bytes.len() + 1;
200-
201-
Ok(SocketAddr { addr: storage, len: length as _ })
167+
sockaddr_un(path.as_ref()).map(|(addr, len)| SocketAddr { addr, len })
202168
}
203169

204170
/// Returns `true` if the address is unnamed.

0 commit comments

Comments
 (0)