Skip to content

Commit cd2909a

Browse files
committed
Peek error
1 parent fddd1ce commit cd2909a

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

boring/src/error.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,32 @@ impl Error {
152152
/// Pops the first error off the OpenSSL error stack.
153153
#[must_use = "Use ErrorStack::clear() to drop the error stack"]
154154
#[corresponds(ERR_get_error_line_data)]
155+
#[inline]
155156
pub fn get() -> Option<Error> {
157+
Self::get_(false)
158+
}
159+
160+
/// Use [`ErrorStack::clear()`] or [`ErrorStack::get()`] afterwards
161+
#[corresponds(ERR_peek_last_error_line_data)]
162+
#[inline]
163+
pub fn peek() -> Option<Error> {
164+
Self::get_(true)
165+
}
166+
167+
fn get_(peek: bool) -> Option<Error> {
156168
unsafe {
157169
ffi::init();
158170

159171
let mut file = ptr::null();
160172
let mut line = 0;
161173
let mut data = ptr::null();
162174
let mut flags = 0;
163-
match ffi::ERR_get_error_line_data(&mut file, &mut line, &mut data, &mut flags) {
175+
let code = if !peek {
176+
ffi::ERR_get_error_line_data(&mut file, &mut line, &mut data, &mut flags)
177+
} else {
178+
ffi::ERR_peek_last_error_line_data(&mut file, &mut line, &mut data, &mut flags)
179+
};
180+
match code {
164181
0 => None,
165182
code => {
166183
// The memory referenced by data is only valid until that slot is overwritten

boring/src/ssl/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4078,7 +4078,9 @@ impl<S: Read + Write> SslStream<S> {
40784078
if e.code() == ErrorCode::SSL {
40794079
if let Some(stack) = e.ssl_error() {
40804080
if let Some(first) = stack.errors().first() {
4081-
if first.code() as i32 == boring_sys::SSL_R_PROTOCOL_IS_SHUTDOWN {
4081+
if first.library_reason(ffi::ERR_LIB_SSL)
4082+
== Some(ffi::SSL_R_PROTOCOL_IS_SHUTDOWN)
4083+
{
40824084
return Ok(ShutdownResult::Received);
40834085
}
40844086
}

boring/src/x509/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::asn1::{
2828
};
2929
use crate::bio::{MemBio, MemBioSlice};
3030
use crate::conf::ConfRef;
31-
use crate::error::ErrorStack;
31+
use crate::error::{Error as PackedError, ErrorStack};
3232
use crate::ex_data::Index;
3333
use crate::hash::{DigestBytes, MessageDigest};
3434
use crate::nid::Nid;
@@ -809,10 +809,9 @@ impl X509 {
809809
let r =
810810
ffi::PEM_read_bio_X509(bio.as_ptr(), ptr::null_mut(), None, ptr::null_mut());
811811
if r.is_null() {
812-
let err = ffi::ERR_peek_last_error();
813-
814-
if ffi::ERR_GET_LIB(err) == ffi::ERR_LIB_PEM.0.try_into().unwrap()
815-
&& ffi::ERR_GET_REASON(err) == ffi::PEM_R_NO_START_LINE
812+
if PackedError::peek()
813+
.and_then(|err| err.library_reason(ffi::ERR_LIB_PEM))
814+
.is_some_and(|code| code == ffi::PEM_R_NO_START_LINE)
816815
{
817816
ErrorStack::clear();
818817
break;

0 commit comments

Comments
 (0)