comparison libpam-sys/libpam-sys-helpers/src/memory.rs @ 143:ebb71a412b58

Turn everything into OsString and Just Walk Out! for strings with nul. To reduce the hazard surface of the API, this replaces most uses of &str with &OsStr (and likewise with String/OsString). Also, I've decided that instead of dealing with callers putting `\0` in their parameters, I'm going to follow the example of std::env and Just Walk Out! (i.e., panic!()). This makes things a lot less annoying for both me and (hopefully) users.
author Paul Fisher <paul@pfish.zone>
date Sat, 05 Jul 2025 22:12:46 -0400
parents add7228adb2f
children
comparison
equal deleted inserted replaced
142:5c1e315c18ff 143:ebb71a412b58
369 /// - The borrowed data must not outlive the pointer's validity. 369 /// - The borrowed data must not outlive the pointer's validity.
370 pub unsafe fn contents<'a>(ptr: *const Self) -> (&'a [u8], u8) { 370 pub unsafe fn contents<'a>(ptr: *const Self) -> (&'a [u8], u8) {
371 let header: &Self = ptr.as_ref().unwrap_unchecked(); 371 let header: &Self = ptr.as_ref().unwrap_unchecked();
372 (&Self::buffer_of(ptr)[5..], header.data_type) 372 (&Self::buffer_of(ptr)[5..], header.data_type)
373 } 373 }
374 374
375 /// Zeroes out the data of this payload. 375 /// Zeroes out the data of this payload.
376 /// 376 ///
377 /// # Safety 377 /// # Safety
378 /// 378 ///
379 /// - The pointer must point to a valid `BinaryPayload`. 379 /// - The pointer must point to a valid `BinaryPayload`.
380 /// - The binary payload must not be used in the future, 380 /// - The binary payload must not be used in the future,
381 /// since its length metadata is gone and so its buffer is unknown. 381 /// since its length metadata is gone and so its buffer is unknown.
382 pub unsafe fn zero(ptr: *mut Self) { 382 pub unsafe fn zero(ptr: *mut Self) {
383 let size = Self::total_bytes(ptr); 383 let size = Self::total_bytes(ptr);
453 /// 453 ///
454 /// You must provide a valid pointer, allocated by (or equivalent to one 454 /// You must provide a valid pointer, allocated by (or equivalent to one
455 /// allocated by) [`Self::new`]. For instance, passing a pointer allocated 455 /// allocated by) [`Self::new`]. For instance, passing a pointer allocated
456 /// by `malloc` to `OwnedBinaryPayload::<Vec<u8>>::from_ptr` is not allowed. 456 /// by `malloc` to `OwnedBinaryPayload::<Vec<u8>>::from_ptr` is not allowed.
457 pub unsafe fn from_ptr(ptr: NonNull<BinaryPayload>) -> Self { 457 pub unsafe fn from_ptr(ptr: NonNull<BinaryPayload>) -> Self {
458 Self(O::from_ptr(ptr.cast(), BinaryPayload::total_bytes(ptr.as_ptr()))) 458 Self(O::from_ptr(
459 ptr.cast(),
460 BinaryPayload::total_bytes(ptr.as_ptr()),
461 ))
459 } 462 }
460 } 463 }
461 464
462 #[cfg(test)] 465 #[cfg(test)]
463 mod tests { 466 mod tests {