comparison src/libpam/conversation.rs @ 89:dd3e9c4bcde3

Simplify memory management in Questions. When we're sending Questions to the client, we don't need them to be C-managed, we just need the pointers going to the right place. This replaces a bunch of Question management cruft with Vecs and Boxes.
author Paul Fisher <paul@pfish.zone>
date Fri, 13 Jun 2025 05:22:48 -0400
parents 05291b601f0a
children efc2b56c8928
comparison
equal deleted inserted replaced
88:c9fc7e6257d3 89:dd3e9c4bcde3
3 use crate::libpam::answer::BinaryAnswer; 3 use crate::libpam::answer::BinaryAnswer;
4 use crate::libpam::answer::{Answer, Answers, TextAnswer}; 4 use crate::libpam::answer::{Answer, Answers, TextAnswer};
5 use crate::libpam::memory::Immovable; 5 use crate::libpam::memory::Immovable;
6 use crate::libpam::pam_ffi::AppData; 6 use crate::libpam::pam_ffi::AppData;
7 pub use crate::libpam::pam_ffi::LibPamConversation; 7 pub use crate::libpam::pam_ffi::LibPamConversation;
8 use crate::libpam::question::{Indirect, IndirectTrait, Question, Questions}; 8 use crate::libpam::question::QuestionsTrait;
9 use crate::libpam::question::{Question, Questions};
9 use crate::ErrorCode; 10 use crate::ErrorCode;
10 use crate::Result; 11 use crate::Result;
11 use std::ffi::c_int; 12 use std::ffi::c_int;
12 use std::iter; 13 use std::iter;
13 use std::marker::PhantomData; 14 use std::marker::PhantomData;
36 // Collect all our pointers 37 // Collect all our pointers
37 let conv = me 38 let conv = me
38 .cast::<C>() 39 .cast::<C>()
39 .as_mut() 40 .as_mut()
40 .ok_or(ErrorCode::ConversationError)?; 41 .ok_or(ErrorCode::ConversationError)?;
41 let indirect = Indirect::borrow_ptr(questions).ok_or(ErrorCode::ConversationError)?; 42 let indirect = Questions::borrow_ptr(questions, count as usize);
42 let answers_ptr = answers.as_mut().ok_or(ErrorCode::ConversationError)?; 43 let answers_ptr = answers.as_mut().ok_or(ErrorCode::ConversationError)?;
43 44
44 // Build our owned list of Q&As from the questions we've been asked 45 // Build our owned list of Q&As from the questions we've been asked
45 let messages: Vec<OwnedMessage> = indirect 46 let messages: Vec<OwnedMessage> = indirect
46 .iter(count as usize)
47 .map(TryInto::try_into) 47 .map(TryInto::try_into)
48 .collect::<Result<_>>() 48 .collect::<Result<_>>()
49 .map_err(|_| ErrorCode::ConversationError)?; 49 .map_err(|_| ErrorCode::ConversationError)?;
50 // Borrow all those Q&As and ask them. 50 // Borrow all those Q&As and ask them.
51 // If we got an invalid message type, bail before sending. 51 // If we got an invalid message type, bail before sending.
69 let mut response_pointer = std::ptr::null_mut(); 69 let mut response_pointer = std::ptr::null_mut();
70 // SAFETY: We're calling into PAM with valid everything. 70 // SAFETY: We're calling into PAM with valid everything.
71 let result = unsafe { 71 let result = unsafe {
72 (self.callback)( 72 (self.callback)(
73 messages.len() as c_int, 73 messages.len() as c_int,
74 questions.indirect(), 74 questions.ptr(),
75 &mut response_pointer, 75 &mut response_pointer,
76 self.appdata, 76 self.appdata,
77 ) 77 )
78 }; 78 };
79 ErrorCode::result_from(result)?; 79 ErrorCode::result_from(result)?;