aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/error.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2018-02-24 20:46:03 -0800
committerSteven Fackler <[email protected]>2018-02-24 20:46:03 -0800
commit5fd23d38d523c3be3138acb4aa2849e271d15b16 (patch)
tree44b0ec1b02d26264e75aa57411a526a2db73cccb /openssl/src/error.rs
parentMerge pull request #847 from sfackler/version2 (diff)
downloadrust-openssl-5fd23d38d523c3be3138acb4aa2849e271d15b16.tar.xz
rust-openssl-5fd23d38d523c3be3138acb4aa2849e271d15b16.zip
Add the ability to push errors back onto the error stack.
Diffstat (limited to 'openssl/src/error.rs')
-rw-r--r--openssl/src/error.rs49
1 files changed, 45 insertions, 4 deletions
diff --git a/openssl/src/error.rs b/openssl/src/error.rs
index 30558912..7f3d472e 100644
--- a/openssl/src/error.rs
+++ b/openssl/src/error.rs
@@ -15,7 +15,7 @@
//! Err(e) => println!("Parsing Error: {:?}", e),
//! }
//! ```
-use libc::{c_char, c_ulong};
+use libc::{c_char, c_int, c_ulong};
use std::fmt;
use std::error;
use std::ffi::CStr;
@@ -41,6 +41,13 @@ impl ErrorStack {
}
ErrorStack(vec)
}
+
+ /// Pushes the errors back onto the OpenSSL error stack.
+ pub fn put(&self) {
+ for error in self.errors() {
+ error.put();
+ }
+ }
}
impl ErrorStack {
@@ -87,7 +94,7 @@ impl From<ErrorStack> for fmt::Error {
pub struct Error {
code: c_ulong,
file: *const c_char,
- line: u32,
+ line: c_int,
data: Option<Cow<'static, str>>,
}
@@ -124,7 +131,7 @@ impl Error {
Some(Error {
code,
file,
- line: line as u32,
+ line,
data,
})
}
@@ -132,6 +139,40 @@ impl Error {
}
}
+ /// Pushes the error back onto the OpenSSL error stack.
+ pub fn put(&self) {
+ unsafe {
+ ffi::ERR_put_error(
+ ffi::ERR_GET_LIB(self.code),
+ ffi::ERR_GET_FUNC(self.code),
+ ffi::ERR_GET_REASON(self.code),
+ self.file,
+ self.line,
+ );
+ let data = match self.data {
+ Some(Cow::Borrowed(data)) => Some((data.as_ptr() as *mut c_char, 0)),
+ Some(Cow::Owned(ref data)) => {
+ let ptr = ffi::CRYPTO_malloc(
+ (data.len() + 1) as _,
+ concat!(file!(), "\0").as_ptr() as _,
+ line!() as _,
+ ) as *mut c_char;
+ if ptr.is_null() {
+ None
+ } else {
+ ptr::copy_nonoverlapping(data.as_ptr(), ptr as *mut u8, data.len());
+ *ptr.offset(data.len() as isize) = 0;
+ Some((ptr, ffi::ERR_TXT_MALLOCED))
+ }
+ }
+ None => None,
+ };
+ if let Some((ptr, flags)) = data {
+ ffi::ERR_set_error_data(ptr, flags | ffi::ERR_TXT_STRING);
+ }
+ }
+ }
+
/// Returns the raw OpenSSL error code for this error.
pub fn code(&self) -> c_ulong {
self.code
@@ -184,7 +225,7 @@ impl Error {
/// Returns the line in the source file which encountered the error.
pub fn line(&self) -> u32 {
- self.line
+ self.line as u32
}
/// Returns additional data describing the error.