ErrorStack ctor for custom errors
This commit is contained in:
parent
e3998212ed
commit
5957ce94cc
|
|
@ -38,6 +38,9 @@ pub struct ErrorStack(Vec<Error>);
|
||||||
|
|
||||||
impl ErrorStack {
|
impl ErrorStack {
|
||||||
/// Pops the contents of the OpenSSL error stack, and returns it.
|
/// Pops the contents of the OpenSSL error stack, and returns it.
|
||||||
|
///
|
||||||
|
/// This should be used only immediately after calling Boring FFI functions,
|
||||||
|
/// otherwise the stack may be empty or a leftover from unrelated calls.
|
||||||
#[corresponds(ERR_get_error_line_data)]
|
#[corresponds(ERR_get_error_line_data)]
|
||||||
#[must_use = "Use ErrorStack::clear() to drop the error stack"]
|
#[must_use = "Use ErrorStack::clear() to drop the error stack"]
|
||||||
pub fn get() -> ErrorStack {
|
pub fn get() -> ErrorStack {
|
||||||
|
|
@ -62,6 +65,12 @@ impl ErrorStack {
|
||||||
Self(vec![Error::new_internal(Data::String(err.to_string()))])
|
Self(vec![Error::new_internal(Data::String(err.to_string()))])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Used to report errors from the Rust crate
|
||||||
|
#[cold]
|
||||||
|
pub(crate) fn internal_error_str(message: &'static str) -> Self {
|
||||||
|
Self(vec![Error::new_internal(Data::Static(message))])
|
||||||
|
}
|
||||||
|
|
||||||
/// Empties the current thread's error queue.
|
/// Empties the current thread's error queue.
|
||||||
#[corresponds(ERR_clear_error)]
|
#[corresponds(ERR_clear_error)]
|
||||||
pub(crate) fn clear() {
|
pub(crate) fn clear() {
|
||||||
|
|
@ -131,6 +140,7 @@ enum Data {
|
||||||
None,
|
None,
|
||||||
CString(CString),
|
CString(CString),
|
||||||
String(String),
|
String(String),
|
||||||
|
Static(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Sync for Error {}
|
unsafe impl Sync for Error {}
|
||||||
|
|
@ -293,6 +303,7 @@ impl Error {
|
||||||
Data::None => None,
|
Data::None => None,
|
||||||
Data::CString(cstring) => cstring.to_str().ok(),
|
Data::CString(cstring) => cstring.to_str().ok(),
|
||||||
Data::String(s) => Some(s),
|
Data::String(s) => Some(s),
|
||||||
|
Data::Static(s) => Some(s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -302,6 +313,7 @@ impl Error {
|
||||||
Data::None => return None,
|
Data::None => return None,
|
||||||
Data::CString(cstr) => return Some(Cow::Borrowed(cstr)),
|
Data::CString(cstr) => return Some(Cow::Borrowed(cstr)),
|
||||||
Data::String(s) => s.as_str(),
|
Data::String(s) => s.as_str(),
|
||||||
|
Data::Static(s) => s,
|
||||||
};
|
};
|
||||||
CString::new(s).ok().map(Cow::Owned)
|
CString::new(s).ok().map(Cow::Owned)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -767,7 +767,7 @@ impl<'a> CryptoBufferBuilder<'a> {
|
||||||
let buffer_capacity = unsafe { ffi::CRYPTO_BUFFER_len(self.buffer) };
|
let buffer_capacity = unsafe { ffi::CRYPTO_BUFFER_len(self.buffer) };
|
||||||
if self.cursor.position() != buffer_capacity as u64 {
|
if self.cursor.position() != buffer_capacity as u64 {
|
||||||
// Make sure all bytes in buffer initialized as required by Boring SSL.
|
// Make sure all bytes in buffer initialized as required by Boring SSL.
|
||||||
return Err(ErrorStack::get());
|
return Err(ErrorStack::internal_error_str("invalid len"));
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut result = ptr::null_mut();
|
let mut result = ptr::null_mut();
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ impl CipherCtxRef {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
||||||
if key.len() != cipher.key_len() {
|
if key.len() != cipher.key_len() {
|
||||||
return Err(ErrorStack::get());
|
return Err(ErrorStack::internal_error_str("invalid key size"));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -117,7 +117,7 @@ impl CipherCtxRef {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
||||||
if key.len() != cipher.key_len() {
|
if key.len() != cipher.key_len() {
|
||||||
return Err(ErrorStack::get());
|
return Err(ErrorStack::internal_error_str("invalid key size"));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,8 @@ where
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(Ok(len)) => len as c_int,
|
Ok(Ok(len)) => len as c_int,
|
||||||
Ok(Err(_)) => {
|
Ok(Err(err)) => {
|
||||||
// FIXME restore error stack
|
err.put();
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue