Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/cstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ macro_rules! cstr {
// We don't use std's `CStr::from_bytes_with_nul`; as of this writing,
// that function isn't defined as `#[inline]` in std and doesn't
// constant-fold away.
assert!(
!$str.bytes().any(|b| b == b'\0'),
::core::assert!(
!::core::iter::Iterator::any(&mut ::core::primitive::str::bytes($str), |b| b == b'\0'),
"cstr argument contains embedded NUL bytes",
);

Expand All @@ -51,7 +51,9 @@ macro_rules! cstr {
// contain embedded NULs above, and we append or own NUL terminator
// here.
unsafe {
$crate::ffi::CStr::from_bytes_with_nul_unchecked(concat!($str, "\0").as_bytes())
$crate::ffi::CStr::from_bytes_with_nul_unchecked(
::core::concat!($str, "\0").as_bytes(),
)
}
}
}};
Expand Down Expand Up @@ -83,4 +85,25 @@ mod tests {
fn test_invalid_empty_cstr() {
let _ = cstr!("\0");
}

#[no_implicit_prelude]
mod hygiene {
#[allow(unused_macros)]
#[test]
fn macro_hygiene() {
macro_rules! assert {
($($tt:tt)*) => {
::core::panic!("cstr! called the wrong assert! macro");
};
}
macro_rules! concat {
($($tt:tt)*) => {{
let v: &str = ::core::panic!("cstr! called the wrong concat! macro");
v
}};
}

let _ = cstr!("foo");
}
}
}
47 changes: 43 additions & 4 deletions src/net/send_recv/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ macro_rules! cmsg_space {
};
(TxTime($len:expr)) => {
$crate::net::__cmsg_space(
$len * ::core::mem::size_of::<u64>(),
$len * ::core::mem::size_of::<::core::primitive::u64>(),
)
};

Expand Down Expand Up @@ -101,15 +101,15 @@ macro_rules! cmsg_aligned_space {
};
(TxTime($len:expr)) => {
$crate::net::__cmsg_aligned_space(
$len * ::core::mem::size_of::<u64>(),
$len * ::core::mem::size_of::<::core::primitive::u64>(),
)
};

// Combo Rules
($firstid:ident($firstex:expr), $($restid:ident($restex:expr)),*) => {{
let sum = cmsg_aligned_space!($firstid($firstex));
let sum = $crate::cmsg_aligned_space!($firstid($firstex));
$(
let sum = sum + cmsg_aligned_space!($restid($restex));
let sum = sum + $crate::cmsg_aligned_space!($restid($restex));
)*
sum
}};
Expand Down Expand Up @@ -985,3 +985,42 @@ mod messages {

impl FusedIterator for Messages<'_> {}
}

#[cfg(test)]
mod tests {
#[no_implicit_prelude]
mod hygiene {
#[allow(unused_macros)]
#[test]
fn macro_hygiene() {
// This `u64` is `!Sized`, so `cmsg_space!` will fail if it tries to get its size with
// `size_of()`.
#[allow(dead_code, non_camel_case_types)]
struct u64([u8]);

// Ensure that when `cmsg*_space!` calls itself recursively, it really calls itself and
// not these macros.
macro_rules! cmsg_space {
($($tt:tt)*) => {{
let v: usize = ::core::panic!("Wrong cmsg_space! macro called");
v
}};
}
macro_rules! cmsg_aligned_space {
($($tt:tt)*) => {{
let v: usize = ::core::panic!("Wrong cmsg_aligned_space! macro called");
v
}};
}

crate::cmsg_space!(ScmRights(1));
crate::cmsg_space!(TxTime(1));
#[cfg(linux_kernel)]
{
crate::cmsg_space!(ScmCredentials(1));
crate::cmsg_space!(ScmRights(1), ScmCredentials(1), TxTime(1));
crate::cmsg_aligned_space!(ScmRights(1), ScmCredentials(1), TxTime(1));
}
}
}
}
Loading