Rollup merge of #130861 - cuviper:sun-path-offset, r=ibraheemdev · qinheping/verify-rust-std@be1d9d6 (original) (raw)

`@@ -15,15 +15,12 @@ mod libc {

`

15

15

`pub type socklen_t = u32;

`

16

16

`pub struct sockaddr;

`

17

17

`#[derive(Clone)]

`

18

``

`-

pub struct sockaddr_un;

`

``

18

`+

pub struct sockaddr_un {

`

``

19

`+

pub sun_path: [u8; 1],

`

``

20

`+

}

`

19

21

`}

`

20

22

``

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);

`

27

24

``

28

25

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

`

29

26

`` // 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

`

53

50

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

`

54

51

`};

`

55

52

``

56

``

`-

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

`

``

53

`+

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

`

57

54

`match bytes.get(0) {

`

58

55

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

`

59

56

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

`

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

`

114

111

`let sun_path: &[u8] =

`

115

112

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

`

116

113

` 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);

`

118

115

`}

`

119

116

``

120

117

`if len == 0 {

`

121

118

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

`

122

119

`// 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

`

124

121

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

`

125

122

`return Err(io::const_io_error!(

`

126

123

` io::ErrorKind::InvalidInput,

`

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

`

238

235

`}

`

239

236

``

240

237

`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;

`

242

239

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

`

243

240

``

244

241

`// 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 {

`

287

284

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

`

288

285

` name.len(),

`

289

286

`);

`

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;

`

291

288

`SocketAddr::from_parts(addr, len)

`

292

289

`}

`

293

290

`}

`