Mercurial > crates > nonstick
comparison libpam-sys/libpam-sys-helpers/src/memory.rs @ 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 | ebb71a412b58 |
comparison
equal
deleted
inserted
replaced
139:33b9622ed6d2 | 140:add7228adb2f |
---|---|
197 #[cfg(not(pam_impl = "LinuxPam"))] | 197 #[cfg(not(pam_impl = "LinuxPam"))] |
198 return Self::iter_over_xsso(ptr_ptr, count); | 198 return Self::iter_over_xsso(ptr_ptr, count); |
199 } | 199 } |
200 | 200 |
201 fn assert_size<That>() { | 201 fn assert_size<That>() { |
202 debug_assert_eq!( | 202 assert_eq!( |
203 mem::size_of::<T>(), | 203 mem::size_of::<T>(), |
204 mem::size_of::<That>(), | 204 mem::size_of::<That>(), |
205 "type {t} is not the size of {that}", | 205 "type {t} is not the size of {that}", |
206 t = any::type_name::<T>(), | 206 t = any::type_name::<T>(), |
207 that = any::type_name::<That>(), | 207 that = any::type_name::<That>(), |
237 /// This is intended to allow you to bring your own allocator for | 237 /// This is intended to allow you to bring your own allocator for |
238 /// [`OwnedBinaryPayload`]s. | 238 /// [`OwnedBinaryPayload`]s. |
239 /// | 239 /// |
240 /// For an implementation example, see the implementation of this trait | 240 /// For an implementation example, see the implementation of this trait |
241 /// for [`Vec`]. | 241 /// for [`Vec`]. |
242 #[allow(clippy::wrong_self_convention)] | |
242 pub trait Buffer { | 243 pub trait Buffer { |
243 /// Allocates a buffer of `len` elements, filled with the default. | 244 /// Allocates a buffer of `len` elements, filled with the default. |
244 fn allocate(len: usize) -> Self; | 245 fn allocate(len: usize) -> Self; |
245 | 246 |
246 fn as_ptr(this: &Self) -> *const u8; | 247 fn as_ptr(this: &Self) -> *const u8; |
303 | 304 |
304 /// The structure of the "binary message" payload for the `PAM_BINARY_PROMPT` | 305 /// The structure of the "binary message" payload for the `PAM_BINARY_PROMPT` |
305 /// extension from Linux-PAM. | 306 /// extension from Linux-PAM. |
306 pub struct BinaryPayload { | 307 pub struct BinaryPayload { |
307 /// The total byte size of the message, including this header, | 308 /// The total byte size of the message, including this header, |
308 /// as a u32 in network byte order (big endian). | 309 /// as u32 in network byte order (big endian). |
309 pub total_bytes_u32be: [u8; 4], | 310 pub total_bytes_u32be: [u8; 4], |
310 /// A tag used to provide some kind of hint as to what the data is. | 311 /// A tag used to provide some kind of hint as to what the data is. |
311 /// Its meaning is undefined. | 312 /// Its meaning is undefined. |
312 pub data_type: u8, | 313 pub data_type: u8, |
313 /// Where the data itself would start, used as a marker to make this | 314 /// Where the data itself would start, used as a marker to make this |
512 let msg = PtrPtrVec::new(bad_vec); | 513 let msg = PtrPtrVec::new(bad_vec); |
513 let _ = msg.as_ptr::<u64>(); | 514 let _ = msg.as_ptr::<u64>(); |
514 } | 515 } |
515 | 516 |
516 #[allow(deprecated)] | 517 #[allow(deprecated)] |
517 #[cfg(debug_assertions)] | |
518 #[test] | 518 #[test] |
519 #[should_panic] | 519 #[should_panic] |
520 fn test_iter_xsso_wrong_size() { | 520 fn test_iter_xsso_wrong_size() { |
521 unsafe { | 521 unsafe { |
522 let _ = PtrPtrVec::<u8>::iter_over_xsso::<f64>(ptr::null(), 1); | 522 let _ = PtrPtrVec::<u8>::iter_over_xsso::<f64>(ptr::null(), 1); |
523 } | 523 } |
524 } | 524 } |
525 | 525 |
526 #[allow(deprecated)] | 526 #[allow(deprecated)] |
527 #[cfg(debug_assertions)] | |
528 #[test] | 527 #[test] |
529 #[should_panic] | 528 #[should_panic] |
530 fn test_iter_linux_wrong_size() { | 529 fn test_iter_linux_wrong_size() { |
531 unsafe { | 530 unsafe { |
532 let _ = PtrPtrVec::<u128>::iter_over_linux::<()>(ptr::null(), 1); | 531 let _ = PtrPtrVec::<u128>::iter_over_linux::<()>(ptr::null(), 1); |
554 } | 553 } |
555 | 554 |
556 #[allow(deprecated)] | 555 #[allow(deprecated)] |
557 #[test] | 556 #[test] |
558 fn test_iter_ptr_ptr() { | 557 fn test_iter_ptr_ptr() { |
559 let strs = vec![Box::new("a"), Box::new("b"), Box::new("c"), Box::new("D")]; | 558 // These boxes are larger than a single pointer because we want to |
560 let ptr: *const *const &str = strs.as_ptr().cast(); | 559 // make sure they're not accidentally allocated adjacently |
560 // in such a way that it's compatible with X/SSO. | |
561 // | |
562 // a pointer to (&str, i32) can be treated as a pointer to (&str). | |
563 #[repr(C)] | |
564 struct pair(&'static str, i32); | |
565 let boxes = vec![ | |
566 Box::new(pair("a", 1)), | |
567 Box::new(pair("b", 2)), | |
568 Box::new(pair("c", 3)), | |
569 Box::new(pair("D", 4)), | |
570 ]; | |
571 let ptr: *const *const &str = boxes.as_ptr().cast(); | |
561 let got: Vec<&str> = unsafe { PtrPtrVec::iter_over_linux(ptr, 4) } | 572 let got: Vec<&str> = unsafe { PtrPtrVec::iter_over_linux(ptr, 4) } |
562 .cloned() | 573 .cloned() |
563 .collect(); | 574 .collect(); |
564 assert_eq!(vec!["a", "b", "c", "D"], got); | 575 assert_eq!(vec!["a", "b", "c", "D"], got); |
565 | 576 |
577 // On the other hand, we explicitly want these to be adjacent. | |
566 let nums = [-1i8, 2, 3]; | 578 let nums = [-1i8, 2, 3]; |
567 let ptr = nums.as_ptr(); | 579 let ptr = nums.as_ptr(); |
568 let got: Vec<u8> = unsafe { PtrPtrVec::iter_over_xsso(&ptr, 3) } | 580 let got: Vec<u8> = unsafe { PtrPtrVec::iter_over_xsso(&ptr, 3) } |
569 .cloned() | 581 .cloned() |
570 .collect(); | 582 .collect(); |