Skip to content

Commit 329f9fc

Browse files
Rollup merge of #130861 - cuviper:sun-path-offset, r=ibraheemdev
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 c9478ef + 9431d1a commit 329f9fc

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ mod libc {
1515
pubtypesocklen_t = u32;
1616
pubstructsockaddr;
1717
#[derive(Clone)]
18-
pubstructsockaddr_un;
18+
pubstructsockaddr_un{
19+
pubsun_path:[u8;1],
20+
}
1921
}
2022

21-
fnsun_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+
constSUN_PATH_OFFSET:usize = mem::offset_of!(libc::sockaddr_un, sun_path);
2724

2825
pub(super)fnsockaddr_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-
letmut len = sun_path_offset(&addr) + bytes.len();
53+
letmut 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_OFFSETas libc::socklen_t;// i.e., zero-length address
124121
}elseif addr.sun_family != libc::AF_UNIXas libc::sa_family_t{
125122
returnErr(io::const_io_error!(
126123
io::ErrorKind::InvalidInput,
@@ -238,7 +235,7 @@ impl SocketAddr {
238235
}
239236

240237
fnaddress(&self) -> AddressKind<'_>{
241-
let len = self.lenasusize - sun_path_offset(&self.addr);
238+
let len = self.lenasusize - 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*mutu8,
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)