From 95f62204fabff8ebe6e24550874cad420aadeeef Mon Sep 17 00:00:00 2001 From: Xavier Moffett Date: Sat, 21 Sep 2024 17:55:48 -0400 Subject: [PATCH] Encapsulate return type with `Option` enum instead of panicking It's possible for libalpm to return valid errors without a pointer. Therefore change the return type for the `data()` function in the `PrepareError` and `CommitError` structs to be encapsulated within an `Option` enum in order to prevent a fatal panic, instead returning `None` when an error is unhandled and thus contains no data. --- alpm/src/trans.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/alpm/src/trans.rs b/alpm/src/trans.rs index 24ee749..a9f1934 100644 --- a/alpm/src/trans.rs +++ b/alpm/src/trans.rs @@ -78,18 +78,24 @@ impl<'a> PrepareError<'a> { self.error } - pub fn data(&self) -> PrepareData { + pub fn data(&self) -> Option { match self.error { Error::PkgInvalidArch => unsafe { - PrepareData::PkgInvalidArch(AlpmListMut::from_ptr(self.data)) + Some(PrepareData::PkgInvalidArch(AlpmListMut::from_ptr( + self.data, + ))) }, Error::UnsatisfiedDeps => unsafe { - PrepareData::UnsatisfiedDeps(AlpmListMut::from_ptr(self.data)) + Some(PrepareData::UnsatisfiedDeps(AlpmListMut::from_ptr( + self.data, + ))) }, Error::ConflictingDeps => unsafe { - PrepareData::ConflictingDeps(AlpmListMut::from_ptr(self.data)) + Some(PrepareData::ConflictingDeps(AlpmListMut::from_ptr( + self.data, + ))) }, - _ => unsafe { unreachable_unchecked() }, + _ => None, } } } @@ -133,15 +139,15 @@ impl CommitError { self.error } - pub fn data(&self) -> CommitData { + pub fn data(&self) -> Option { match self.error { Error::FileConflicts => unsafe { - CommitData::FileConflict(AlpmListMut::from_ptr(self.data)) + Some(CommitData::FileConflict(AlpmListMut::from_ptr(self.data))) }, Error::PkgInvalid | Error::PkgInvalidSig | Error::PkgInvalidChecksum => unsafe { - CommitData::PkgInvalid(AlpmListMut::from_ptr(self.data)) + Some(CommitData::PkgInvalid(AlpmListMut::from_ptr(self.data))) }, - _ => unsafe { unreachable_unchecked() }, + _ => None, } } }