aboutsummaryrefslogtreecommitdiff
path: root/openssl/src
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2016-10-26 21:42:09 -0700
committerSteven Fackler <[email protected]>2016-10-26 21:42:09 -0700
commit654f0941e18376d37c9e0f97d28f44f6b16cd2b5 (patch)
tree0812f41edba34706abff586f3aa182ed1b90b919 /openssl/src
parentMove SslString to a shared location (diff)
downloadrust-openssl-654f0941e18376d37c9e0f97d28f44f6b16cd2b5.tar.xz
rust-openssl-654f0941e18376d37c9e0f97d28f44f6b16cd2b5.zip
Don't double-allocate strings
Diffstat (limited to 'openssl/src')
-rw-r--r--openssl/src/bn.rs23
-rw-r--r--openssl/src/crypto.rs11
2 files changed, 17 insertions, 17 deletions
diff --git a/openssl/src/bn.rs b/openssl/src/bn.rs
index 700367bd..fb0c286c 100644
--- a/openssl/src/bn.rs
+++ b/openssl/src/bn.rs
@@ -1,11 +1,12 @@
use ffi;
-use libc::{c_int, c_void};
+use libc::c_int;
use std::cmp::Ordering;
-use std::ffi::{CStr, CString};
+use std::ffi::CString;
use std::{fmt, ptr};
use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub, Deref, DerefMut};
use {cvt, cvt_p, cvt_n};
+use crypto::CryptoString;
use error::ErrorStack;
use opaque::Opaque;
@@ -473,15 +474,12 @@ impl BigNumRef {
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(12345).unwrap();
///
- /// assert_eq!(s.to_dec_str().unwrap(), "-12345");
+ /// assert_eq!(&*s.to_dec_str().unwrap(), "-12345");
/// ```
- pub fn to_dec_str(&self) -> Result<String, ErrorStack> {
+ pub fn to_dec_str(&self) -> Result<CryptoString, ErrorStack> {
unsafe {
let buf = try!(cvt_p(ffi::BN_bn2dec(self.as_ptr())));
- let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec())
- .unwrap();
- CRYPTO_free!(buf as *mut c_void);
- Ok(str)
+ Ok(CryptoString::from_null_terminated(buf))
}
}
@@ -491,15 +489,12 @@ impl BigNumRef {
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(0x99ff).unwrap();
///
- /// assert_eq!(s.to_hex_str().unwrap(), "-99FF");
+ /// assert_eq!(&*s.to_hex_str().unwrap(), "-99FF");
/// ```
- pub fn to_hex_str(&self) -> Result<String, ErrorStack> {
+ pub fn to_hex_str(&self) -> Result<CryptoString, ErrorStack> {
unsafe {
let buf = try!(cvt_p(ffi::BN_bn2hex(self.as_ptr())));
- let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec())
- .unwrap();
- CRYPTO_free!(buf as *mut c_void);
- Ok(str)
+ Ok(CryptoString::from_null_terminated(buf))
}
}
}
diff --git a/openssl/src/crypto.rs b/openssl/src/crypto.rs
index 0a121fa0..26f66d0e 100644
--- a/openssl/src/crypto.rs
+++ b/openssl/src/crypto.rs
@@ -1,6 +1,6 @@
-use ffi;
-use libc::{c_int, c_void};
+use libc::{c_char, c_void};
use std::fmt;
+use std::ffi::CStr;
use std::slice;
use std::ops::Deref;
use std::str;
@@ -24,10 +24,15 @@ impl Deref for CryptoString {
}
impl CryptoString {
- pub unsafe fn from_raw_parts(buf: *const u8, len: usize) -> CryptoString {
+ pub unsafe fn from_raw_parts(buf: *mut u8, len: usize) -> CryptoString {
let slice = slice::from_raw_parts(buf, len);
CryptoString(str::from_utf8_unchecked(slice))
}
+
+ pub unsafe fn from_null_terminated(buf: *mut c_char) -> CryptoString {
+ let slice = CStr::from_ptr(buf).to_bytes();
+ CryptoString(str::from_utf8_unchecked(slice))
+ }
}
impl fmt::Display for CryptoString {