Use mem::offset_of! for sockaddr_un.sun_path · qinheping/verify-rust-std@3d03634 (original) (raw)

`@@ -18,12 +18,7 @@ mod libc {

`

18

18

`pub struct sockaddr_un;

`

19

19

`}

`

20

20

``

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

``

`-

}

`

``

21

`+

const SUN_PATH_OFFSET: usize = mem::offset_of!(libc::sockaddr_un, sun_path);

`

27

22

``

28

23

`pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {

`

29

24

`` // SAFETY: All zeros is a valid representation for sockaddr_un.

``

`@@ -53,7 +48,7 @@ pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::s

`

53

48

` ptr::copy_nonoverlapping(bytes.as_ptr(), addr.sun_path.as_mut_ptr().cast(), bytes.len())

`

54

49

`};

`

55

50

``

56

``

`-

let mut len = sun_path_offset(&addr) + bytes.len();

`

``

51

`+

let mut len = SUN_PATH_OFFSET + bytes.len();

`

57

52

`match bytes.get(0) {

`

58

53

`Some(&0) | None => {}

`

59

54

`Some(_) => len += 1,

`

`@@ -114,13 +109,13 @@ impl SocketAddr {

`

114

109

`let sun_path: &[u8] =

`

115

110

`unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) };

`

116

111

` len = core::slice::memchr::memchr(0, sun_path)

`

117

``

`-

.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);

`

``

112

`+

.map_or(len, |new_len| (new_len + SUN_PATH_OFFSET) as libc::socklen_t);

`

118

113

`}

`

119

114

``

120

115

`if len == 0 {

`

121

116

`// When there is a datagram from unnamed unix socket

`

122

117

`// linux returns zero bytes of address

`

123

``

`-

len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address

`

``

118

`+

len = SUN_PATH_OFFSET as libc::socklen_t; // i.e., zero-length address

`

124

119

`} else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {

`

125

120

`return Err(io::const_io_error!(

`

126

121

` io::ErrorKind::InvalidInput,

`

`@@ -238,7 +233,7 @@ impl SocketAddr {

`

238

233

`}

`

239

234

``

240

235

`fn address(&self) -> AddressKind<'_> {

`

241

``

`-

let len = self.len as usize - sun_path_offset(&self.addr);

`

``

236

`+

let len = self.len as usize - SUN_PATH_OFFSET;

`

242

237

`let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };

`

243

238

``

244

239

`// macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses

`

`@@ -287,7 +282,7 @@ impl linux_ext::addr::SocketAddrExt for SocketAddr {

`

287

282

` addr.sun_path.as_mut_ptr().add(1) as *mut u8,

`

288

283

` name.len(),

`

289

284

`);

`

290

``

`-

let len = (sun_path_offset(&addr) + 1 + name.len()) as libc::socklen_t;

`

``

285

`+

let len = (SUN_PATH_OFFSET + 1 + name.len()) as libc::socklen_t;

`

291

286

`SocketAddr::from_parts(addr, len)

`

292

287

`}

`

293

288

`}

`