Skip to content

Commit f30f592

Browse files
committed
Auto merge of rust-lang#130861 - cuviper:sun-path-offset, r=<try>
Use `mem::offset_of!` for `sockaddr_un.sun_path` We don't need manual pointer math here anymore! try-job: dist-i686-msvc
2 parents 4428a05 + 9431d1a commit f30f592

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

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

+9-12
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ mod libc {
1515
pub type socklen_t = u32;
1616
pub struct sockaddr;
1717
#[derive(Clone)]
18-
pub struct sockaddr_un;
18+
pub struct sockaddr_un {
19+
pub sun_path: [u8; 1],
20+
}
1921
}
2022

21-
fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
22-
// Work with an actual instance of the type since using a null pointer is UB
23-
let base = (addr as *const libc::sockaddr_un).addr();
24-
let path = core::ptr::addr_of!(addr.sun_path).addr();
25-
path - base
26-
}
23+
const SUN_PATH_OFFSET: usize = mem::offset_of!(libc::sockaddr_un, sun_path);
2724

2825
pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
2926
// SAFETY: All zeros is a valid representation for `sockaddr_un`.
@@ -53,7 +50,7 @@ pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::s
5350
ptr::copy_nonoverlapping(bytes.as_ptr(), addr.sun_path.as_mut_ptr().cast(), bytes.len())
5451
};
5552

56-
let mut len = sun_path_offset(&addr) + bytes.len();
53+
let mut len = SUN_PATH_OFFSET + bytes.len();
5754
match bytes.get(0) {
5855
Some(&0) | None => {}
5956
Some(_) => len += 1,
@@ -114,13 +111,13 @@ impl SocketAddr {
114111
let sun_path: &[u8] =
115112
unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) };
116113
len = core::slice::memchr::memchr(0, sun_path)
117-
.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);
114+
.map_or(len, |new_len| (new_len + SUN_PATH_OFFSET) as libc::socklen_t);
118115
}
119116

120117
if len == 0 {
121118
// When there is a datagram from unnamed unix socket
122119
// linux returns zero bytes of address
123-
len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
120+
len = SUN_PATH_OFFSET as libc::socklen_t; // i.e., zero-length address
124121
} else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
125122
return Err(io::const_io_error!(
126123
io::ErrorKind::InvalidInput,
@@ -238,7 +235,7 @@ impl SocketAddr {
238235
}
239236

240237
fn address(&self) -> AddressKind<'_> {
241-
let len = self.len as usize - sun_path_offset(&self.addr);
238+
let len = self.len as usize - SUN_PATH_OFFSET;
242239
let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
243240

244241
// macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
@@ -287,7 +284,7 @@ impl linux_ext::addr::SocketAddrExt for SocketAddr {
287284
addr.sun_path.as_mut_ptr().add(1) as *mut u8,
288285
name.len(),
289286
);
290-
let len = (sun_path_offset(&addr) + 1 + name.len()) as libc::socklen_t;
287+
let len = (SUN_PATH_OFFSET + 1 + name.len()) as libc::socklen_t;
291288
SocketAddr::from_parts(addr, len)
292289
}
293290
}

0 commit comments

Comments
 (0)