Merge pull request #146 from elichai/2018-08-unintialized

Remove all usage of `mem::uninitialized()`
This commit is contained in:
Andrew Poelstra 2019-08-19 17:29:53 +00:00 committed by GitHub
commit a055c3fdc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 27 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "secp256k1" name = "secp256k1"
version = "0.15.2" version = "0.15.3"
authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>", authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>",
"Andrew Poelstra <apoelstra@wpsoftware.net>" ] "Andrew Poelstra <apoelstra@wpsoftware.net>" ]
license = "CC0-1.0" license = "CC0-1.0"

View File

@ -30,7 +30,7 @@ impl SharedSecret {
#[inline] #[inline]
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret { pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
unsafe { unsafe {
let mut ss = ffi::SharedSecret::blank(); let mut ss = ffi::SharedSecret::new();
let res = ffi::secp256k1_ecdh( let res = ffi::secp256k1_ecdh(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
&mut ss, &mut ss,

View File

@ -77,7 +77,8 @@ impl PublicKey {
/// Create a new (zeroed) public key usable for the FFI interface /// Create a new (zeroed) public key usable for the FFI interface
pub fn new() -> PublicKey { PublicKey([0; 64]) } pub fn new() -> PublicKey { PublicKey([0; 64]) }
/// Create a new (uninitialized) public key usable for the FFI interface /// Create a new (uninitialized) public key usable for the FFI interface
pub unsafe fn blank() -> PublicKey { mem::uninitialized() } #[deprecated(since = "0.15.3", note = "Please use the new function instead")]
pub unsafe fn blank() -> PublicKey { PublicKey::new() }
} }
impl Default for PublicKey { impl Default for PublicKey {
@ -102,7 +103,8 @@ impl Signature {
/// Create a new (zeroed) signature usable for the FFI interface /// Create a new (zeroed) signature usable for the FFI interface
pub fn new() -> Signature { Signature([0; 64]) } pub fn new() -> Signature { Signature([0; 64]) }
/// Create a new (uninitialized) signature usable for the FFI interface /// Create a new (uninitialized) signature usable for the FFI interface
pub unsafe fn blank() -> Signature { mem::uninitialized() } #[deprecated(since = "0.15.3", note = "Please use the new function instead")]
pub unsafe fn blank() -> Signature { Signature::new() }
} }
impl Default for Signature { impl Default for Signature {
@ -121,7 +123,8 @@ impl SharedSecret {
/// Create a new (zeroed) signature usable for the FFI interface /// Create a new (zeroed) signature usable for the FFI interface
pub fn new() -> SharedSecret { SharedSecret([0; 32]) } pub fn new() -> SharedSecret { SharedSecret([0; 32]) }
/// Create a new (uninitialized) signature usable for the FFI interface /// Create a new (uninitialized) signature usable for the FFI interface
pub unsafe fn blank() -> SharedSecret { mem::uninitialized() } #[deprecated(since = "0.15.3", note = "Please use the new function instead")]
pub unsafe fn blank() -> SharedSecret { SharedSecret::new() }
} }
impl Default for SharedSecret { impl Default for SharedSecret {

View File

@ -17,7 +17,7 @@
#[cfg(any(test, feature = "rand"))] use rand::Rng; #[cfg(any(test, feature = "rand"))] use rand::Rng;
use core::{fmt, mem, str}; use core::{fmt, str};
use super::{from_hex, Secp256k1}; use super::{from_hex, Secp256k1};
use super::Error::{self, InvalidPublicKey, InvalidSecretKey}; use super::Error::{self, InvalidPublicKey, InvalidSecretKey};
@ -219,7 +219,7 @@ impl PublicKey {
pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>, pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>,
sk: &SecretKey) sk: &SecretKey)
-> PublicKey { -> PublicKey {
let mut pk = unsafe { ffi::PublicKey::blank() }; let mut pk = ffi::PublicKey::new();
unsafe { unsafe {
// We can assume the return value because it's not possible to construct // We can assume the return value because it's not possible to construct
// an invalid `SecretKey` without transmute trickery or something // an invalid `SecretKey` without transmute trickery or something
@ -232,7 +232,7 @@ impl PublicKey {
/// Creates a public key directly from a slice /// Creates a public key directly from a slice
#[inline] #[inline]
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> { pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
let mut pk = unsafe { ffi::PublicKey::blank() }; let mut pk = ffi::PublicKey::new();
unsafe { unsafe {
if ffi::secp256k1_ec_pubkey_parse( if ffi::secp256k1_ec_pubkey_parse(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
@ -338,7 +338,7 @@ impl PublicKey {
/// to its own negation /// to its own negation
pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error> { pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error> {
unsafe { unsafe {
let mut ret = mem::uninitialized(); let mut ret = ffi::PublicKey::new();
let ptrs = [self.as_ptr(), other.as_ptr()]; let ptrs = [self.as_ptr(), other.as_ptr()];
if ffi::secp256k1_ec_pubkey_combine( if ffi::secp256k1_ec_pubkey_combine(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,

View File

@ -246,7 +246,7 @@ impl Signature {
#[inline] #[inline]
/// Converts a DER-encoded byte slice to a signature /// Converts a DER-encoded byte slice to a signature
pub fn from_der(data: &[u8]) -> Result<Signature, Error> { pub fn from_der(data: &[u8]) -> Result<Signature, Error> {
let mut ret = unsafe { ffi::Signature::blank() }; let mut ret = ffi::Signature::new();
unsafe { unsafe {
if ffi::secp256k1_ecdsa_signature_parse_der( if ffi::secp256k1_ecdsa_signature_parse_der(
@ -265,7 +265,7 @@ impl Signature {
/// Converts a 64-byte compact-encoded byte slice to a signature /// Converts a 64-byte compact-encoded byte slice to a signature
pub fn from_compact(data: &[u8]) -> Result<Signature, Error> { pub fn from_compact(data: &[u8]) -> Result<Signature, Error> {
let mut ret = unsafe { ffi::Signature::blank() }; let mut ret = ffi::Signature::new();
if data.len() != 64 { if data.len() != 64 {
return Err(Error::InvalidSignature) return Err(Error::InvalidSignature)
} }
@ -290,7 +290,7 @@ impl Signature {
/// support serializing to this "format" /// support serializing to this "format"
pub fn from_der_lax(data: &[u8]) -> Result<Signature, Error> { pub fn from_der_lax(data: &[u8]) -> Result<Signature, Error> {
unsafe { unsafe {
let mut ret = ffi::Signature::blank(); let mut ret = ffi::Signature::new();
if ffi::ecdsa_signature_parse_der_lax( if ffi::ecdsa_signature_parse_der_lax(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
&mut ret, &mut ret,
@ -605,7 +605,7 @@ impl<C: Signing> Secp256k1<C> {
pub fn sign(&self, msg: &Message, sk: &key::SecretKey) pub fn sign(&self, msg: &Message, sk: &key::SecretKey)
-> Signature { -> Signature {
let mut ret = unsafe { ffi::Signature::blank() }; let mut ret = ffi::Signature::new();
unsafe { unsafe {
// We can assume the return value because it's not possible to construct // We can assume the return value because it's not possible to construct
// an invalid signature from a valid `Message` and `SecretKey` // an invalid signature from a valid `Message` and `SecretKey`

View File

@ -68,15 +68,8 @@ macro_rules! impl_array_newtype {
impl Clone for $thing { impl Clone for $thing {
#[inline] #[inline]
fn clone(&self) -> $thing { fn clone(&self) -> $thing {
unsafe { let &$thing(ref dat) = self;
use core::intrinsics::copy_nonoverlapping; $thing(dat.clone())
use core::mem;
let mut ret: $thing = mem::uninitialized();
copy_nonoverlapping(self.as_ptr(),
ret.as_mut_ptr(),
$len);
ret
}
} }
} }

View File

@ -29,7 +29,8 @@ impl RecoverableSignature {
/// Create a new (zeroed) signature usable for the FFI interface /// Create a new (zeroed) signature usable for the FFI interface
pub fn new() -> RecoverableSignature { RecoverableSignature([0; 65]) } pub fn new() -> RecoverableSignature { RecoverableSignature([0; 65]) }
/// Create a new (uninitialized) signature usable for the FFI interface /// Create a new (uninitialized) signature usable for the FFI interface
pub unsafe fn blank() -> RecoverableSignature { mem::uninitialized() } #[deprecated(since = "0.15.3", note = "Please use the new function instead")]
pub unsafe fn blank() -> RecoverableSignature { RecoverableSignature::new() }
} }
impl Default for RecoverableSignature { impl Default for RecoverableSignature {

View File

@ -57,7 +57,7 @@ impl RecoverableSignature {
/// representation is nonstandard and defined by the libsecp256k1 /// representation is nonstandard and defined by the libsecp256k1
/// library. /// library.
pub fn from_compact(data: &[u8], recid: RecoveryId) -> Result<RecoverableSignature, Error> { pub fn from_compact(data: &[u8], recid: RecoveryId) -> Result<RecoverableSignature, Error> {
let mut ret = unsafe { ffi::RecoverableSignature::blank() }; let mut ret = ffi::RecoverableSignature::new();
unsafe { unsafe {
if data.len() != 64 { if data.len() != 64 {
@ -103,7 +103,7 @@ impl RecoverableSignature {
/// for verification /// for verification
#[inline] #[inline]
pub fn to_standard(&self) -> Signature { pub fn to_standard(&self) -> Signature {
let mut ret = unsafe { super_ffi::Signature::blank() }; let mut ret = super_ffi::Signature::new();
unsafe { unsafe {
let err = ffi::secp256k1_ecdsa_recoverable_signature_convert( let err = ffi::secp256k1_ecdsa_recoverable_signature_convert(
super_ffi::secp256k1_context_no_precomp, super_ffi::secp256k1_context_no_precomp,
@ -130,7 +130,7 @@ impl<C: Signing> Secp256k1<C> {
pub fn sign_recoverable(&self, msg: &Message, sk: &key::SecretKey) pub fn sign_recoverable(&self, msg: &Message, sk: &key::SecretKey)
-> RecoverableSignature { -> RecoverableSignature {
let mut ret = unsafe { ffi::RecoverableSignature::blank() }; let mut ret = ffi::RecoverableSignature::new();
unsafe { unsafe {
// We can assume the return value because it's not possible to construct // We can assume the return value because it's not possible to construct
// an invalid signature from a valid `Message` and `SecretKey` // an invalid signature from a valid `Message` and `SecretKey`
@ -157,7 +157,7 @@ impl<C: Verification> Secp256k1<C> {
pub fn recover(&self, msg: &Message, sig: &RecoverableSignature) pub fn recover(&self, msg: &Message, sig: &RecoverableSignature)
-> Result<key::PublicKey, Error> { -> Result<key::PublicKey, Error> {
let mut pk = unsafe { super_ffi::PublicKey::blank() }; let mut pk = super_ffi::PublicKey::new();
unsafe { unsafe {
if ffi::secp256k1_ecdsa_recover(self.ctx, &mut pk, if ffi::secp256k1_ecdsa_recover(self.ctx, &mut pk,