changeset 140:add7228adb2f

Neaten up some stuff in libpam-sys memory module.
author Paul Fisher <paul@pfish.zone>
date Sat, 05 Jul 2025 17:11:33 -0400
parents 33b9622ed6d2
children a508a69c068a
files libpam-sys/libpam-sys-helpers/src/memory.rs
diffstat 1 files changed, 18 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libpam-sys/libpam-sys-helpers/src/memory.rs	Thu Jul 03 23:57:49 2025 -0400
+++ b/libpam-sys/libpam-sys-helpers/src/memory.rs	Sat Jul 05 17:11:33 2025 -0400
@@ -199,7 +199,7 @@
     }
 
     fn assert_size<That>() {
-        debug_assert_eq!(
+        assert_eq!(
             mem::size_of::<T>(),
             mem::size_of::<That>(),
             "type {t} is not the size of {that}",
@@ -239,6 +239,7 @@
 ///
 /// For an implementation example, see the implementation of this trait
 /// for [`Vec`].
+#[allow(clippy::wrong_self_convention)]
 pub trait Buffer {
     /// Allocates a buffer of `len` elements, filled with the default.
     fn allocate(len: usize) -> Self;
@@ -305,7 +306,7 @@
 /// extension from Linux-PAM.
 pub struct BinaryPayload {
     /// The total byte size of the message, including this header,
-    /// as a u32 in network byte order (big endian).
+    /// as u32 in network byte order (big endian).
     pub total_bytes_u32be: [u8; 4],
     /// A tag used to provide some kind of hint as to what the data is.
     /// Its meaning is undefined.
@@ -514,7 +515,6 @@
     }
 
     #[allow(deprecated)]
-    #[cfg(debug_assertions)]
     #[test]
     #[should_panic]
     fn test_iter_xsso_wrong_size() {
@@ -524,7 +524,6 @@
     }
 
     #[allow(deprecated)]
-    #[cfg(debug_assertions)]
     #[test]
     #[should_panic]
     fn test_iter_linux_wrong_size() {
@@ -556,13 +555,26 @@
     #[allow(deprecated)]
     #[test]
     fn test_iter_ptr_ptr() {
-        let strs = vec![Box::new("a"), Box::new("b"), Box::new("c"), Box::new("D")];
-        let ptr: *const *const &str = strs.as_ptr().cast();
+        // These boxes are larger than a single pointer because we want to
+        // make sure they're not accidentally allocated adjacently
+        // in such a way that it's compatible with X/SSO.
+        //
+        // a pointer to (&str, i32) can be treated as a pointer to (&str).
+        #[repr(C)]
+        struct pair(&'static str, i32);
+        let boxes = vec![
+            Box::new(pair("a", 1)),
+            Box::new(pair("b", 2)),
+            Box::new(pair("c", 3)),
+            Box::new(pair("D", 4)),
+        ];
+        let ptr: *const *const &str = boxes.as_ptr().cast();
         let got: Vec<&str> = unsafe { PtrPtrVec::iter_over_linux(ptr, 4) }
             .cloned()
             .collect();
         assert_eq!(vec!["a", "b", "c", "D"], got);
 
+        // On the other hand, we explicitly want these to be adjacent.
         let nums = [-1i8, 2, 3];
         let ptr = nums.as_ptr();
         let got: Vec<u8> = unsafe { PtrPtrVec::iter_over_xsso(&ptr, 3) }