Skip to content

Commit 987c8a6

Browse files
committed
Use memoffset::offset_of instead of homegrown macro
The homegrown macro was fine in 2016, but at some point it technically became UB. The memoffset crate does the same thing, but avoids UB when using rustc 1.51.0 or later. Fixes #1415
1 parent 6af11c1 commit 987c8a6

File tree

4 files changed

+5
-13
lines changed

4 files changed

+5
-13
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ libc = { version = "0.2.82", features = [ "extra_traits" ] }
3636
bitflags = "1.1"
3737
cfg-if = "1.0"
3838

39+
[target.'cfg(not(target_os = "redox"))'.dependencies]
40+
memoffset = "0.6.3"
41+
3942
[target.'cfg(target_os = "dragonfly")'.build-dependencies]
4043
cc = "1"
4144

src/macros.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,3 @@ macro_rules! libc_enum {
203203
}
204204
};
205205
}
206-
207-
/// A Rust version of the familiar C `offset_of` macro. It returns the byte
208-
/// offset of `field` within struct `ty`
209-
#[cfg(not(target_os = "redox"))]
210-
macro_rules! offset_of {
211-
($ty:ty, $field:ident) => {{
212-
// Safe because we don't actually read from the dereferenced pointer
213-
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
214-
unsafe {
215-
&(*(ptr::null() as *const $ty)).$field as *const _ as usize
216-
}
217-
}}
218-
}

src/sys/socket/addr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::sa_family_t;
22
use crate::{Error, Result, NixPath};
33
use crate::errno::Errno;
4+
use memoffset::offset_of;
45
use std::{fmt, mem, net, ptr, slice};
56
use std::ffi::OsStr;
67
use std::hash::{Hash, Hasher};

src/sys/socket/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use cfg_if::cfg_if;
55
use crate::{Error, Result, errno::Errno};
66
use libc::{self, c_void, c_int, iovec, socklen_t, size_t,
77
CMSG_FIRSTHDR, CMSG_NXTHDR, CMSG_DATA, CMSG_LEN};
8+
use memoffset::offset_of;
89
use std::{mem, ptr, slice};
910
use std::os::unix::io::RawFd;
1011
use crate::sys::time::TimeVal;

0 commit comments

Comments
 (0)