rust: bssl-tls: parse_all_from_pem should recover from BIO failure Signed-off-by: Xiangfei Ding <xfding@google.com> Change-Id: I7cf2f800ad74f3624f840066f43c931f6a6a6964 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/101411 Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Benjamin <davidben@google.com>
diff --git a/rust/bssl-tls/src/credentials.rs b/rust/bssl-tls/src/credentials.rs index 106df17..61fb5a6 100644 --- a/rust/bssl-tls/src/credentials.rs +++ b/rust/bssl-tls/src/credentials.rs
@@ -476,7 +476,7 @@ cert: &[u8], cache: Option<&CertificateCache>, ) -> Result<Vec<Self>, Error> { - let mut bio = Bio::from_bytes(cert).unwrap(); + let mut bio = Bio::from_bytes(cert); let mut res = vec![]; loop { match Self::parse_one(&mut bio, cache) { @@ -497,7 +497,7 @@ cert: &[u8], cache: Option<&CertificateCache>, ) -> Result<Self, Error> { - let mut bio = Bio::from_bytes(cert)?; + let mut bio = Bio::from_bytes(cert); let (cert, _) = Self::parse_one(&mut bio, cache)?; Ok(cert) }
diff --git a/rust/bssl-tls/src/ffi.rs b/rust/bssl-tls/src/ffi.rs index ae6397a..5ca3bd0 100644 --- a/rust/bssl-tls/src/ffi.rs +++ b/rust/bssl-tls/src/ffi.rs
@@ -33,10 +33,7 @@ use crate::{ context::CertificateCache, - errors::{ - Error, - IoError, // - }, // + errors::Error, // }; pub(crate) fn slice_into_ffi_raw_parts<T>(slice: &[T]) -> (*const T, usize) { @@ -97,21 +94,18 @@ Bio(bio, PhantomData) } - pub fn from_bytes(buf: &'a [u8]) -> Result<Self, Error> { - let len = if let Ok(len) = buf.len().try_into() { - len - } else { - return Err(Error::Io(IoError::TooLong)); - }; + pub fn from_bytes(buf: &'a [u8]) -> Self { + #[allow(clippy::expect_used, reason = "breach of fundamental invariant")] + let len = buf.len().try_into().expect("impossible allocation size"); let mem_buf = unsafe { // Safety: buf is still valid bssl_sys::BIO_new_mem_buf(buf.as_ffi_void_ptr(), len) }; let mem_buf = NonNull::new(mem_buf).expect("allocation failure"); - Ok(unsafe { + unsafe { // Safety: our returned object is outlived by the input buffer. Self::new(mem_buf) - }) + } } pub fn ptr(&mut self) -> *mut bssl_sys::BIO {