Skip to content

Commit c0a9fd7

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 e266b7c commit c0a9fd7

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
@@ -35,6 +35,9 @@ libc = { version = "0.2.99", features = [ "extra_traits" ] }
3535
bitflags = ">= 1.1.0, < 1.3.0"
3636
cfg-if = "1.0"
3737

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

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)