Use ERR_clear_error

This commit is contained in:
Kornel 2025-06-13 13:05:24 +01:00 committed by Kornel
parent a91bfdc67d
commit 50fa2e672f
3 changed files with 17 additions and 5 deletions

View File

@ -35,7 +35,8 @@ pub struct ErrorStack(Vec<Error>);
impl ErrorStack { impl ErrorStack {
/// Pops the contents of the OpenSSL error stack, and returns it. /// Pops the contents of the OpenSSL error stack, and returns it.
#[allow(clippy::must_use_candidate)] #[corresponds(ERR_get_error_line_data)]
#[must_use = "Use ErrorStack::clear() to drop the error stack"]
pub fn get() -> ErrorStack { pub fn get() -> ErrorStack {
let mut vec = vec![]; let mut vec = vec![];
while let Some(err) = Error::get() { while let Some(err) = Error::get() {
@ -45,6 +46,7 @@ impl ErrorStack {
} }
/// Pushes the errors back onto the OpenSSL error stack. /// Pushes the errors back onto the OpenSSL error stack.
#[corresponds(ERR_put_error)]
pub fn put(&self) { pub fn put(&self) {
for error in self.errors() { for error in self.errors() {
error.put(); error.put();
@ -56,6 +58,14 @@ impl ErrorStack {
pub(crate) fn internal_error(err: impl error::Error) -> Self { pub(crate) fn internal_error(err: impl error::Error) -> Self {
Self(vec![Error::new_internal(err.to_string())]) Self(vec![Error::new_internal(err.to_string())])
} }
/// Empties the current thread's error queue.
#[corresponds(ERR_clear_error)]
pub(crate) fn clear() {
unsafe {
ffi::ERR_clear_error();
}
}
} }
impl ErrorStack { impl ErrorStack {
@ -120,7 +130,8 @@ static BORING_INTERNAL: &CStr = c"boring-rust";
impl Error { impl Error {
/// Pops the first error off the OpenSSL error stack. /// Pops the first error off the OpenSSL error stack.
#[allow(clippy::must_use_candidate)] #[must_use = "Use ErrorStack::clear() to drop the error stack"]
#[corresponds(ERR_get_error_line_data)]
pub fn get() -> Option<Error> { pub fn get() -> Option<Error> {
unsafe { unsafe {
ffi::init(); ffi::init();
@ -153,6 +164,7 @@ impl Error {
} }
/// Pushes the error back onto the OpenSSL error stack. /// Pushes the error back onto the OpenSSL error stack.
#[corresponds(ERR_put_error)]
pub fn put(&self) { pub fn put(&self) {
unsafe { unsafe {
ffi::ERR_put_error( ffi::ERR_put_error(

View File

@ -478,7 +478,7 @@ impl<'a> Verifier<'a> {
match r { match r {
1 => Ok(true), 1 => Ok(true),
0 => { 0 => {
ErrorStack::get(); // discard error stack ErrorStack::clear(); // discard error stack
Ok(false) Ok(false)
} }
_ => Err(ErrorStack::get()), _ => Err(ErrorStack::get()),
@ -500,7 +500,7 @@ impl<'a> Verifier<'a> {
match r { match r {
1 => Ok(true), 1 => Ok(true),
0 => { 0 => {
ErrorStack::get(); ErrorStack::clear();
Ok(false) Ok(false)
} }
_ => Err(ErrorStack::get()), _ => Err(ErrorStack::get()),

View File

@ -815,7 +815,7 @@ impl X509 {
if ffi::ERR_GET_LIB(err) == ffi::ERR_LIB_PEM.0.try_into().unwrap() if ffi::ERR_GET_LIB(err) == ffi::ERR_LIB_PEM.0.try_into().unwrap()
&& ffi::ERR_GET_REASON(err) == ffi::PEM_R_NO_START_LINE && ffi::ERR_GET_REASON(err) == ffi::PEM_R_NO_START_LINE
{ {
ffi::ERR_clear_error(); ErrorStack::clear();
break; break;
} }