diff src/libpam/question.rs @ 141:a508a69c068a

Remove a lot of Results from functions. Many functions are documented to only return failing Results when given improper inputs or when there is a memory allocation failure (which can be verified by looking at the source). In cases where we know our input is correct, we don't need to check for memory allocation errors for the same reason that Rust doesn't do so when you, e.g., create a new Vec.
author Paul Fisher <paul@pfish.zone>
date Sat, 05 Jul 2025 17:16:56 -0400
parents 33b9622ed6d2
children ebb71a412b58
line wrap: on
line diff
--- a/src/libpam/question.rs	Sat Jul 05 17:11:33 2025 -0400
+++ b/src/libpam/question.rs	Sat Jul 05 17:16:56 2025 -0400
@@ -1,13 +1,11 @@
 //! Data and types dealing with PAM messages.
 
-#[cfg(feature = "linux-pam-ext")]
-use crate::conv::{BinaryQAndA, RadioQAndA};
-use libpam_sys_helpers::memory::{BinaryPayload, TooBigError};
 use crate::conv::{ErrorMsg, Exchange, InfoMsg, MaskedQAndA, QAndA};
 use crate::libpam::conversation::OwnedExchange;
-use crate::libpam::memory::{CHeapBox, CHeapPayload, CHeapString};
+use crate::libpam::memory;
 use crate::ErrorCode;
 use crate::Result;
+use libpam_sys_helpers::memory as pammem;
 use num_enum::{IntoPrimitive, TryFromPrimitive};
 use std::ffi::{c_int, c_void, CStr};
 use std::ptr::NonNull;
@@ -84,7 +82,7 @@
     unsafe fn binary_data(&self) -> (&[u8], u8) {
         self.data
             .as_ref()
-            .map(|data| BinaryPayload::contents(data.as_ptr().cast()))
+            .map(|data| pammem::BinaryPayload::contents(data.as_ptr().cast()))
             .unwrap_or_default()
     }
 }
@@ -94,11 +92,11 @@
     fn try_from(msg: &Exchange) -> Result<Self> {
         let alloc = |style, text| -> Result<_> {
             Ok((style, unsafe {
-                CHeapBox::cast(CHeapString::new(text)?.into_box())
+                memory::CHeapBox::cast(memory::CHeapString::new(text)?.into_box())
             }))
         };
         // We will only allocate heap data if we have a valid input.
-        let (style, data): (_, CHeapBox<c_void>) = match *msg {
+        let (style, data): (_, memory::CHeapBox<c_void>) = match *msg {
             Exchange::MaskedPrompt(p) => alloc(Style::PromptEchoOff, p.question()),
             Exchange::Prompt(p) => alloc(Style::PromptEchoOn, p.question()),
             Exchange::Error(p) => alloc(Style::ErrorMsg, p.question()),
@@ -108,9 +106,11 @@
             #[cfg(feature = "linux-pam-ext")]
             Exchange::BinaryPrompt(p) => {
                 let (data, typ) = p.question();
-                let payload = CHeapPayload::new(data, typ)?.into_inner();
-                Ok((Style::BinaryPrompt, unsafe { CHeapBox::cast(payload) }))
-            },
+                let payload = memory::CHeapPayload::new(data, typ)?.into_inner();
+                Ok((Style::BinaryPrompt, unsafe {
+                    memory::CHeapBox::cast(payload)
+                }))
+            }
             #[cfg(not(feature = "linux-pam-ext"))]
             Exchange::RadioPrompt(_) | Exchange::BinaryPrompt(_) => {
                 Err(ErrorCode::ConversationError)
@@ -118,7 +118,7 @@
         }?;
         Ok(Self {
             style: style.into(),
-            data: Some(CHeapBox::into_ptr(data)),
+            data: Some(memory::CHeapBox::into_ptr(data)),
         })
     }
 }
@@ -136,21 +136,22 @@
                     Style::BinaryPrompt => self
                         .data
                         .as_mut()
-                        .map(|p| BinaryPayload::zero(p.as_ptr().cast())),
+                        .map(|p| pammem::BinaryPayload::zero(p.as_ptr().cast())),
                     #[cfg(feature = "linux-pam-ext")]
                     Style::RadioType => self
                         .data
                         .as_mut()
-                        .map(|p| CHeapString::zero(p.cast())),
+                        .map(|p| memory::CHeapString::zero(p.cast())),
                     Style::TextInfo
                     | Style::ErrorMsg
                     | Style::PromptEchoOff
-                    | Style::PromptEchoOn => {
-                        self.data.as_mut().map(|p| CHeapString::zero(p.cast()))
-                    }
+                    | Style::PromptEchoOn => self
+                        .data
+                        .as_mut()
+                        .map(|p| memory::CHeapString::zero(p.cast())),
                 };
             };
-            let _ = self.data.map(|p| CHeapBox::from_ptr(p));
+            let _ = self.data.map(|p| memory::CHeapBox::from_ptr(p));
         }
     }
 }
@@ -173,9 +174,13 @@
                 Style::ErrorMsg => Self::Error(ErrorMsg::new(question.string_data()?)),
                 Style::TextInfo => Self::Info(InfoMsg::new(question.string_data()?)),
                 #[cfg(feature = "linux-pam-ext")]
-                Style::RadioType => Self::RadioPrompt(RadioQAndA::new(question.string_data()?)),
+                Style::RadioType => {
+                    Self::RadioPrompt(crate::conv::RadioQAndA::new(question.string_data()?))
+                }
                 #[cfg(feature = "linux-pam-ext")]
-                Style::BinaryPrompt => Self::BinaryPrompt(BinaryQAndA::new(question.binary_data())),
+                Style::BinaryPrompt => {
+                    Self::BinaryPrompt(crate::conv::BinaryQAndA::new(question.binary_data()))
+                }
             }
         };
         Ok(prompt)
@@ -183,8 +188,8 @@
 }
 
 #[cfg(feature = "linux-pam-ext")]
-impl From<TooBigError> for ErrorCode {
-    fn from(_: TooBigError) -> Self {
+impl From<pammem::TooBigError> for ErrorCode {
+    fn from(_: pammem::TooBigError) -> Self {
         ErrorCode::BufferError
     }
 }
@@ -225,6 +230,7 @@
     #[test]
     #[cfg(feature = "linux-pam-ext")]
     fn linux_extensions() {
+        use crate::conv::{BinaryQAndA, RadioQAndA};
         assert_matches!(
             (Exchange::BinaryPrompt, (&[5, 4, 3, 2, 1][..], 66)),
             BinaryQAndA::new((&[5, 4, 3, 2, 1], 66))