summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-04-04 15:12:55 +0000
committerGitHub <noreply@github.com>2021-04-04 15:12:55 +0000
commitb3f935923dee7fd8de2ff4aff6ef7776207b39d8 (patch)
tree64d5beb795e4cfbb6077226f5210753783dfd8af /src
parentc5d75b8807e6c3359b81bf974e5b40ffe3382d1f (diff)
parent987c8a607b7b5b12660efeb50376ccce6b17e9a6 (diff)
downloadnix-b3f935923dee7fd8de2ff4aff6ef7776207b39d8.zip
Merge #1416
1416: Use memoffset::offset_of instead of homegrown macro r=asomers a=asomers 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 Co-authored-by: Alan Somers <asomers@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/macros.rs13
-rw-r--r--src/sys/socket/addr.rs1
-rw-r--r--src/sys/socket/mod.rs1
3 files changed, 2 insertions, 13 deletions
diff --git a/src/macros.rs b/src/macros.rs
index feb02ea7..7d6ac8df 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -203,16 +203,3 @@ macro_rules! libc_enum {
}
};
}
-
-/// A Rust version of the familiar C `offset_of` macro. It returns the byte
-/// offset of `field` within struct `ty`
-#[cfg(not(target_os = "redox"))]
-macro_rules! offset_of {
- ($ty:ty, $field:ident) => {{
- // Safe because we don't actually read from the dereferenced pointer
- #[allow(unused_unsafe)] // for when the macro is used in an unsafe block
- unsafe {
- &(*(ptr::null() as *const $ty)).$field as *const _ as usize
- }
- }}
-}
diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs
index 1f7f4ec6..71b2c973 100644
--- a/src/sys/socket/addr.rs
+++ b/src/sys/socket/addr.rs
@@ -1,6 +1,7 @@
use super::sa_family_t;
use crate::{Error, Result, NixPath};
use crate::errno::Errno;
+use memoffset::offset_of;
use std::{fmt, mem, net, ptr, slice};
use std::ffi::OsStr;
use std::hash::{Hash, Hasher};
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index 2725e57c..4301ee1f 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -5,6 +5,7 @@ use cfg_if::cfg_if;
use crate::{Error, Result, errno::Errno};
use libc::{self, c_void, c_int, iovec, socklen_t, size_t,
CMSG_FIRSTHDR, CMSG_NXTHDR, CMSG_DATA, CMSG_LEN};
+use memoffset::offset_of;
use std::{mem, ptr, slice};
use std::os::unix::io::RawFd;
use crate::sys::time::TimeVal;