diff src/libpam/conversation.rs @ 98:b87100c5eed4

Start on environment variables, and make pointers nicer. This starts work on the PAM environment handling, and in so doing, introduces the CHeapBox and CHeapString structs. These are analogous to Box and CString, but they're located on the C heap rather than being Rust-managed memory. This is because environment variables deal with even more pointers and it turns out we can lose a lot of manual freeing using homemade smart pointers.
author Paul Fisher <paul@pfish.zone>
date Tue, 24 Jun 2025 04:25:25 -0400
parents efe2f5f8b5b2
children 3f11b8d30f63
line wrap: on
line diff
--- a/src/libpam/conversation.rs	Mon Jun 23 19:10:34 2025 -0400
+++ b/src/libpam/conversation.rs	Tue Jun 24 04:25:25 2025 -0400
@@ -12,6 +12,7 @@
 use std::ffi::c_int;
 use std::iter;
 use std::marker::PhantomData;
+use std::ptr::NonNull;
 use std::result::Result as StdResult;
 
 impl LibPamConversation<'_> {
@@ -55,7 +56,7 @@
 
             // Send our answers back.
             let owned = Answers::build(messages).map_err(|_| ErrorCode::ConversationError)?;
-            *answers_ptr = owned.into_ptr();
+            *answers_ptr = owned.into_ptr().as_ptr();
             Ok(())
         };
         ErrorCode::result_to_c(internal())
@@ -81,6 +82,8 @@
             // We have to trust that the responses from PAM match up
             // with the questions we sent.
             unsafe {
+                let response_pointer =
+                    NonNull::new(response_pointer).ok_or(ErrorCode::ConversationError)?;
                 let mut owned_responses = Answers::from_c_heap(response_pointer, messages.len());
                 for (msg, response) in iter::zip(messages, owned_responses.iter_mut()) {
                     convert(msg, response);