From 64e5d7f7ceb95c01062f0084b401143c54df4f58 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Tue, 24 Aug 2021 12:12:18 +0200 Subject: [PATCH] Change PepIdentity::as_mut to return a Result. - If `PepIdentity::as_mut` is passed a NULL pointer, return an error. --- src/lib.rs | 7 +------ src/pep/identity.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dde929a..3d0c220 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1254,12 +1254,7 @@ ffi!(fn _pgp_generate_keypair(session: *mut Session, let session = Session::as_mut(session)?; let mm = session.mm(); - let identity = if let Some(i) = PepIdentity::as_mut(identity) { - i - } else { - return Err(Error::IllegalValue( - "identity must not be NULL".into())); - }; + let identity = PepIdentity::as_mut(identity)?; t!("identity: {:?}", identity); let is_group_identity diff --git a/src/pep/identity.rs b/src/pep/identity.rs index bbc7a42..5228467 100644 --- a/src/pep/identity.rs +++ b/src/pep/identity.rs @@ -34,6 +34,8 @@ use std::ptr; use sequoia_openpgp as openpgp; use openpgp::Fingerprint; +use crate::Error; +use crate::Result; use crate::buffer::{ malloc_cleared, rust_str_to_c_str, @@ -118,8 +120,13 @@ impl PepIdentity { /// Converts the raw pointer to a Rust reference. /// /// This does not take ownership of the object. - pub fn as_mut(ptr: *mut Self) -> Option<&'static mut Self> { - unsafe { ptr.as_mut() } + pub fn as_mut(ptr: *mut Self) -> Result<&'static mut Self> { + if let Some(identity) = unsafe { ptr.as_mut() } { + Ok(identity) + } else { + Err(Error::IllegalValue( + "PepIdentity may not be NULL".into())) + } } /// Returns the address. -- GitLab