diff --git a/src/cstr.rs b/src/cstr.rs index 75eb61b90..46cde2f87 100644 --- a/src/cstr.rs +++ b/src/cstr.rs @@ -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", ); @@ -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(), + ) } } }}; @@ -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"); + } + } } diff --git a/src/net/send_recv/msg.rs b/src/net/send_recv/msg.rs index 96dad0b4c..234e9933a 100644 --- a/src/net/send_recv/msg.rs +++ b/src/net/send_recv/msg.rs @@ -68,7 +68,7 @@ macro_rules! cmsg_space { }; (TxTime($len:expr)) => { $crate::net::__cmsg_space( - $len * ::core::mem::size_of::(), + $len * ::core::mem::size_of::<::core::primitive::u64>(), ) }; @@ -101,15 +101,15 @@ macro_rules! cmsg_aligned_space { }; (TxTime($len:expr)) => { $crate::net::__cmsg_aligned_space( - $len * ::core::mem::size_of::(), + $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 }}; @@ -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)); + } + } + } +}