2424//! use boring::asn1::Asn1Time;
2525//! let tomorrow = Asn1Time::days_from_now(1);
2626//! ```
27- use crate :: ffi;
2827use foreign_types:: { ForeignType , ForeignTypeRef } ;
2928use libc:: { c_int, c_long, time_t} ;
3029use std:: cmp:: Ordering ;
3130use std:: ffi:: CString ;
3231use std:: fmt;
3332use std:: ptr;
34- use std:: slice;
3533use std:: str;
3634
3735use crate :: bio:: MemBio ;
3836use crate :: bn:: { BigNum , BigNumRef } ;
3937use crate :: error:: ErrorStack ;
38+ use crate :: ffi;
4039use crate :: nid:: Nid ;
4140use crate :: stack:: Stackable ;
4241use crate :: string:: OpensslString ;
43- use crate :: { cvt, cvt_p} ;
42+ use crate :: try_slice;
43+ use crate :: { cvt, cvt_n, cvt_p} ;
4444use openssl_macros:: corresponds;
4545
4646foreign_type_and_impl_send_sync ! {
@@ -403,11 +403,7 @@ impl Asn1StringRef {
403403 pub fn as_utf8 ( & self ) -> Result < OpensslString , ErrorStack > {
404404 unsafe {
405405 let mut ptr = ptr:: null_mut ( ) ;
406- let len = ffi:: ASN1_STRING_to_UTF8 ( & mut ptr, self . as_ptr ( ) ) ;
407- if len < 0 {
408- return Err ( ErrorStack :: get ( ) ) ;
409- }
410-
406+ cvt_n ( ffi:: ASN1_STRING_to_UTF8 ( & mut ptr, self . as_ptr ( ) ) ) ?;
411407 Ok ( OpensslString :: from_ptr ( ptr. cast ( ) ) )
412408 }
413409 }
@@ -421,7 +417,7 @@ impl Asn1StringRef {
421417 #[ corresponds( ASN1_STRING_get0_data ) ]
422418 #[ must_use]
423419 pub fn as_slice ( & self ) -> & [ u8 ] {
424- unsafe { slice :: from_raw_parts ( ASN1_STRING_get0_data ( self . as_ptr ( ) ) , self . len ( ) ) }
420+ unsafe { try_slice ( ASN1_STRING_get0_data ( self . as_ptr ( ) ) , self . len ( ) ) . unwrap_or ( & [ ] ) }
425421 }
426422
427423 /// Returns the number of bytes in the string.
@@ -529,13 +525,7 @@ impl Asn1BitStringRef {
529525 #[ corresponds( ASN1_STRING_get0_data ) ]
530526 #[ must_use]
531527 pub fn as_slice ( & self ) -> & [ u8 ] {
532- unsafe {
533- let ptr = ASN1_STRING_get0_data ( self . as_ptr ( ) . cast ( ) ) ;
534- if ptr. is_null ( ) {
535- return & [ ] ;
536- }
537- slice:: from_raw_parts ( ptr, self . len ( ) )
538- }
528+ unsafe { try_slice ( ASN1_STRING_get0_data ( self . as_ptr ( ) . cast ( ) ) , self . len ( ) ) . unwrap_or ( & [ ] ) }
539529 }
540530
541531 /// Returns the Asn1BitString as a str, if possible.
0 commit comments