Use try_ssl_null!() when relevant

This commit is contained in:
Frank Denis 2015-09-01 09:25:41 +02:00
parent f4bf55faa3
commit 6a2b4402e9
2 changed files with 7 additions and 28 deletions

View File

@ -8,10 +8,7 @@ pub struct DH(*mut ffi::DH);
impl DH {
pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<DH, SslError> {
let dh = unsafe { ffi::DH_new_from_params(p.raw(), g.raw(), q.raw()) };
if dh == ptr::null_mut() {
return Err(SslError::get());
}
let dh = try_ssl_null!(unsafe { ffi::DH_new_from_params(p.raw(), g.raw(), q.raw()) });
mem::forget(p);
mem::forget(g);
mem::forget(q);
@ -20,28 +17,19 @@ impl DH {
#[cfg(feature = "rfc5114")]
pub fn get_1024_160() -> Result<DH, SslError> {
let dh = unsafe { ffi::DH_get_1024_160() };
if dh == ptr::null_mut() {
return Err(SslError::get());
}
let dh = try_ssl_null!(unsafe { ffi::DH_get_1024_160() });
Ok(DH(dh))
}
#[cfg(feature = "rfc5114")]
pub fn get_2048_224() -> Result<DH, SslError> {
let dh = unsafe { ffi::DH_get_2048_224() };
if dh == ptr::null_mut() {
return Err(SslError::get());
}
let dh = try_ssl_null!(unsafe { ffi::DH_get_2048_224() });
Ok(DH(dh))
}
#[cfg(feature = "rfc5114")]
pub fn get_2048_256() -> Result<DH, SslError> {
let dh = unsafe { ffi::DH_get_2048_256() };
if dh == ptr::null_mut() {
return Err(SslError::get());
}
let dh = try_ssl_null!(unsafe { ffi::DH_get_2048_256() });
Ok(DH(dh))
}

View File

@ -432,10 +432,7 @@ impl SslContext {
pub fn new(method: SslMethod) -> Result<SslContext, SslError> {
init();
let ctx = unsafe { ffi::SSL_CTX_new(method.to_raw()) };
if ctx == ptr::null_mut() {
return Err(SslError::get());
}
let ctx = try_ssl_null!(unsafe { ffi::SSL_CTX_new(method.to_raw()) });
let ctx = SslContext { ctx: ctx };
@ -690,10 +687,7 @@ impl Drop for Ssl {
impl Ssl {
pub fn new(ctx: &SslContext) -> Result<Ssl, SslError> {
let ssl = unsafe { ffi::SSL_new(ctx.ctx) };
if ssl == ptr::null_mut() {
return Err(SslError::get());
}
let ssl = try_ssl_null!(unsafe { ffi::SSL_new(ctx.ctx) });
let ssl = Ssl { ssl: ssl };
Ok(ssl)
}
@ -1019,10 +1013,7 @@ impl DirectStream<net::TcpStream> {
impl<S> DirectStream<S> {
fn new_base(ssl: Ssl, stream: S, sock: c_int) -> Result<DirectStream<S>, SslError> {
unsafe {
let bio = ffi::BIO_new_socket(sock, 0);
if bio == ptr::null_mut() {
return Err(SslError::get());
}
let bio = try_ssl_null!(ffi::BIO_new_socket(sock, 0));
ffi::SSL_set_bio(ssl.ssl, bio, bio);
}