diff src/pam_ffi/memory.rs @ 73:ac6881304c78

Do conversations, along with way too much stuff. This implements conversations, along with all the memory management brouhaha that goes along with it. The conversation now lives directly on the handle rather than being a thing you have to get from it and then call manually. It Turns Out this makes things a lot easier! I guess we reorganized things again. For the last time. For real. I promise. This all passes ASAN, so it seems Pretty Good!
author Paul Fisher <paul@pfish.zone>
date Thu, 05 Jun 2025 03:41:38 -0400
parents 47eb242a4f88
children
line wrap: on
line diff
--- a/src/pam_ffi/memory.rs	Wed Jun 04 03:53:36 2025 -0400
+++ b/src/pam_ffi/memory.rs	Thu Jun 05 03:41:38 2025 -0400
@@ -8,7 +8,9 @@
 use std::{ptr, slice};
 
 /// Makes whatever it's in not [`Send`], [`Sync`], or [`Unpin`].
-pub type Immovable = PhantomData<(*mut u8, PhantomPinned)>;
+#[repr(C)]
+#[derive(Debug)]
+pub struct Immovable(pub PhantomData<(*mut u8, PhantomPinned)>);
 
 /// Safely converts a `&str` option to a `CString` option.
 pub fn option_cstr(prompt: Option<&str>) -> Result<Option<CString>> {
@@ -71,7 +73,7 @@
     }
     unsafe {
         let data_alloc = libc::calloc(data.len() + 1, 1);
-        libc::memcpy(data_alloc, data.as_ptr() as *const c_void, data.len());
+        libc::memcpy(data_alloc, data.as_ptr().cast(), data.len());
         Ok(data_alloc.cast())
     }
 }
@@ -85,7 +87,7 @@
 /// It's up to you to provide a valid C string.
 pub unsafe fn zero_c_string(cstr: *mut c_void) {
     if !cstr.is_null() {
-        libc::memset(cstr, 0, libc::strlen(cstr as *const c_char));
+        libc::memset(cstr, 0, libc::strlen(cstr.cast()));
     }
 }
 
@@ -113,17 +115,14 @@
             max: (u32::MAX - 5) as usize,
             actual: source.len(),
         })?;
+        // SAFETY: We're only allocating here.
         let data = unsafe {
-            let dest_buffer = libc::malloc(buffer_size as usize) as *mut CBinaryData;
+            let dest_buffer: *mut CBinaryData = libc::malloc(buffer_size as usize).cast();
             let data = &mut *dest_buffer;
             data.total_length = buffer_size.to_be_bytes();
             data.data_type = data_type;
             let dest = data.data.as_mut_ptr();
-            libc::memcpy(
-                dest as *mut c_void,
-                source.as_ptr() as *const c_void,
-                source.len(),
-            );
+            libc::memcpy(dest.cast(), source.as_ptr().cast(), source.len());
             dest_buffer
         };
         Ok(data)