Skip to content

Commit

Permalink
error: add is_fatal helper, use in verify_cert
Browse files Browse the repository at this point in the history
This commit adds a method to `Error` for testing whether an error should
be considered fatal, e.g. should stop any further path building
progress. The existing consideration of fatal errors in
`loop_while_non_fatal_error` is updated to use the `is_fatal` fn.

Having this in a central place means we can avoid duplicating the match
arms in multiple places, where they are likely to fall out-of-sync.
  • Loading branch information
cpu committed Sep 12, 2023
1 parent 0598dd2 commit 0651f72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ pub enum Error {
UnsupportedSignatureAlgorithmForPublicKey,
}

impl Error {
/// Returns true for errors that should be considered fatal during path building. Errors of
/// this class should halt any further path building and be returned immediately.
#[inline]
pub(crate) fn is_fatal(&self) -> bool {
matches!(
self,
Error::MaximumSignatureChecksExceeded
| Error::MaximumPathBuildCallsExceeded
| Error::MaximumNameConstraintComparisonsExceeded
)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
Expand Down
6 changes: 3 additions & 3 deletions src/verify_cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ where
// If the error is not fatal, then keep going.
match f(v) {
Ok(()) => return Ok(()),
err @ Err(Error::MaximumSignatureChecksExceeded)
| err @ Err(Error::MaximumPathBuildCallsExceeded)
| err @ Err(Error::MaximumNameConstraintComparisonsExceeded) => return err,
// Fatal errors should halt further looping.
res @ Err(err) if err.is_fatal() => return res,
// Non-fatal errors should allow looping to continue.
_ => {}
}
}
Expand Down

0 comments on commit 0651f72

Please sign in to comment.