@@ -198,11 +198,7 @@ impl Error {
198
198
self . line ,
199
199
self . func . as_ref ( ) . map_or ( ptr:: null ( ) , |s| s. as_ptr ( ) ) ,
200
200
) ;
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 ( ) ) ;
206
202
}
207
203
}
208
204
@@ -214,9 +210,9 @@ impl Error {
214
210
let line = self . line . try_into ( ) . unwrap ( ) ;
215
211
unsafe {
216
212
ffi:: ERR_put_error (
217
- ffi :: ERR_GET_LIB ( self . code ) ,
213
+ self . library_code ( ) ,
218
214
ffi:: ERR_GET_FUNC ( self . code ) ,
219
- ffi :: ERR_GET_REASON ( self . code ) ,
215
+ self . reason_code ( ) ,
220
216
self . file . as_ptr ( ) ,
221
217
line,
222
218
) ;
@@ -240,6 +236,15 @@ impl Error {
240
236
}
241
237
}
242
238
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
+
243
248
/// Returns the name of the function reporting the error.
244
249
pub fn function ( & self ) -> Option < RetStr < ' _ > > {
245
250
self . func . as_ref ( ) . map ( |s| s. as_str ( ) )
@@ -257,6 +262,14 @@ impl Error {
257
262
}
258
263
}
259
264
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
+
260
273
/// Returns the name of the source file which encountered the error.
261
274
pub fn file ( & self ) -> RetStr < ' _ > {
262
275
self . file . as_str ( )
@@ -304,17 +317,15 @@ impl fmt::Display for Error {
304
317
write ! ( fmt, "error:{:08X}" , self . code( ) ) ?;
305
318
match self . library ( ) {
306
319
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 ( ) ) ?,
308
321
}
309
322
match self . function ( ) {
310
323
Some ( f) => write ! ( fmt, ":{}" , f) ?,
311
324
None => write ! ( fmt, ":func({})" , unsafe { ffi:: ERR_GET_FUNC ( self . code( ) ) } ) ?,
312
325
}
313
326
match self . reason ( ) {
314
327
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( ) ) ?,
318
329
}
319
330
write ! (
320
331
fmt,
@@ -387,3 +398,18 @@ cfg_if! {
387
398
}
388
399
}
389
400
}
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