Skip to content

Commit 4bc21b0

Browse files
committed
Expose the raw library and reason codes on Error
1 parent 9ea51ec commit 4bc21b0

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

openssl/src/error.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,7 @@ impl Error {
198198
self.line,
199199
self.func.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
200200
);
201-
ffi::ERR_set_error(
202-
ffi::ERR_GET_LIB(self.code),
203-
ffi::ERR_GET_REASON(self.code),
204-
ptr::null(),
205-
);
201+
ffi::ERR_set_error(self.library_code(), self.reason_code(), ptr::null());
206202
}
207203
}
208204

@@ -214,9 +210,9 @@ impl Error {
214210
let line = self.line.try_into().unwrap();
215211
unsafe {
216212
ffi::ERR_put_error(
217-
ffi::ERR_GET_LIB(self.code),
213+
self.library_code(),
218214
ffi::ERR_GET_FUNC(self.code),
219-
ffi::ERR_GET_REASON(self.code),
215+
self.reason_code(),
220216
self.file.as_ptr(),
221217
line,
222218
);
@@ -240,6 +236,15 @@ impl Error {
240236
}
241237
}
242238

239+
/// Returns the raw OpenSSL error constant for the library reporting the
240+
/// error.
241+
// On BoringSSL ERR_GET_{LIB,FUNC,REASON} are `unsafe`, but on
242+
// OpenSSL/LibreSSL they're safe.
243+
#[allow(unused_unsafe)]
244+
pub fn library_code(&self) -> libc::c_int {
245+
unsafe { ffi::ERR_GET_LIB(self.code) }
246+
}
247+
243248
/// Returns the name of the function reporting the error.
244249
pub fn function(&self) -> Option<RetStr<'_>> {
245250
self.func.as_ref().map(|s| s.as_str())
@@ -257,6 +262,14 @@ impl Error {
257262
}
258263
}
259264

265+
/// Returns the raw OpenSSL error constant for the reason for the error.
266+
// On BoringSSL ERR_GET_{LIB,FUNC,REASON} are `unsafe`, but on
267+
// OpenSSL/LibreSSL they're safe.
268+
#[allow(unused_unsafe)]
269+
pub fn reason_code(&self) -> libc::c_int {
270+
unsafe { ffi::ERR_GET_REASON(self.code) }
271+
}
272+
260273
/// Returns the name of the source file which encountered the error.
261274
pub fn file(&self) -> RetStr<'_> {
262275
self.file.as_str()
@@ -304,17 +317,15 @@ impl fmt::Display for Error {
304317
write!(fmt, "error:{:08X}", self.code())?;
305318
match self.library() {
306319
Some(l) => write!(fmt, ":{}", l)?,
307-
None => write!(fmt, ":lib({})", unsafe { ffi::ERR_GET_LIB(self.code()) })?,
320+
None => write!(fmt, ":lib({})", self.library_code())?,
308321
}
309322
match self.function() {
310323
Some(f) => write!(fmt, ":{}", f)?,
311324
None => write!(fmt, ":func({})", unsafe { ffi::ERR_GET_FUNC(self.code()) })?,
312325
}
313326
match self.reason() {
314327
Some(r) => write!(fmt, ":{}", r)?,
315-
None => write!(fmt, ":reason({})", unsafe {
316-
ffi::ERR_GET_REASON(self.code())
317-
})?,
328+
None => write!(fmt, ":reason({})", self.reason_code())?,
318329
}
319330
write!(
320331
fmt,
@@ -387,3 +398,18 @@ cfg_if! {
387398
}
388399
}
389400
}
401+
402+
#[cfg(test)]
403+
mod tests {
404+
use crate::nid::Nid;
405+
406+
#[test]
407+
fn test_error_library_code() {
408+
let stack = Nid::create("not-an-oid", "invalid", "invalid").unwrap_err();
409+
let errors = stack.errors();
410+
#[cfg(not(boringssl))]
411+
assert_eq!(errors[0].library_code(), ffi::ERR_LIB_ASN1);
412+
#[cfg(boringssl)]
413+
assert_eq!(errors[0].library_code(), ffi::ERR_LIB_OBJ as libc::c_int);
414+
}
415+
}

0 commit comments

Comments
 (0)