Skip to content

Commit a055c3f

Browse files
authored
Merge pull request #146 from elichai/2018-08-unintialized
Remove all usage of `mem::uninitialized()`
2 parents dfe7ee5 + f75772b commit a055c3f

File tree

8 files changed

+24
-27
lines changed

8 files changed

+24
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "secp256k1"
4-
version = "0.15.2"
4+
version = "0.15.3"
55
authors = [ "Dawid Ciężarkiewicz <[email protected]>",
66
"Andrew Poelstra <[email protected]>" ]
77
license = "CC0-1.0"

src/ecdh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl SharedSecret {
3030
#[inline]
3131
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
3232
unsafe {
33-
let mut ss = ffi::SharedSecret::blank();
33+
let mut ss = ffi::SharedSecret::new();
3434
let res = ffi::secp256k1_ecdh(
3535
ffi::secp256k1_context_no_precomp,
3636
&mut ss,

src/ffi.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ impl PublicKey {
7777
/// Create a new (zeroed) public key usable for the FFI interface
7878
pub fn new() -> PublicKey { PublicKey([0; 64]) }
7979
/// Create a new (uninitialized) public key usable for the FFI interface
80-
pub unsafe fn blank() -> PublicKey { mem::uninitialized() }
80+
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
81+
pub unsafe fn blank() -> PublicKey { PublicKey::new() }
8182
}
8283

8384
impl Default for PublicKey {
@@ -102,7 +103,8 @@ impl Signature {
102103
/// Create a new (zeroed) signature usable for the FFI interface
103104
pub fn new() -> Signature { Signature([0; 64]) }
104105
/// Create a new (uninitialized) signature usable for the FFI interface
105-
pub unsafe fn blank() -> Signature { mem::uninitialized() }
106+
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
107+
pub unsafe fn blank() -> Signature { Signature::new() }
106108
}
107109

108110
impl Default for Signature {
@@ -121,7 +123,8 @@ impl SharedSecret {
121123
/// Create a new (zeroed) signature usable for the FFI interface
122124
pub fn new() -> SharedSecret { SharedSecret([0; 32]) }
123125
/// Create a new (uninitialized) signature usable for the FFI interface
124-
pub unsafe fn blank() -> SharedSecret { mem::uninitialized() }
126+
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
127+
pub unsafe fn blank() -> SharedSecret { SharedSecret::new() }
125128
}
126129

127130
impl Default for SharedSecret {

src/key.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
#[cfg(any(test, feature = "rand"))] use rand::Rng;
1919

20-
use core::{fmt, mem, str};
20+
use core::{fmt, str};
2121

2222
use super::{from_hex, Secp256k1};
2323
use super::Error::{self, InvalidPublicKey, InvalidSecretKey};
@@ -219,7 +219,7 @@ impl PublicKey {
219219
pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>,
220220
sk: &SecretKey)
221221
-> PublicKey {
222-
let mut pk = unsafe { ffi::PublicKey::blank() };
222+
let mut pk = ffi::PublicKey::new();
223223
unsafe {
224224
// We can assume the return value because it's not possible to construct
225225
// an invalid `SecretKey` without transmute trickery or something
@@ -232,7 +232,7 @@ impl PublicKey {
232232
/// Creates a public key directly from a slice
233233
#[inline]
234234
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
235-
let mut pk = unsafe { ffi::PublicKey::blank() };
235+
let mut pk = ffi::PublicKey::new();
236236
unsafe {
237237
if ffi::secp256k1_ec_pubkey_parse(
238238
ffi::secp256k1_context_no_precomp,
@@ -338,7 +338,7 @@ impl PublicKey {
338338
/// to its own negation
339339
pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error> {
340340
unsafe {
341-
let mut ret = mem::uninitialized();
341+
let mut ret = ffi::PublicKey::new();
342342
let ptrs = [self.as_ptr(), other.as_ptr()];
343343
if ffi::secp256k1_ec_pubkey_combine(
344344
ffi::secp256k1_context_no_precomp,

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl Signature {
246246
#[inline]
247247
/// Converts a DER-encoded byte slice to a signature
248248
pub fn from_der(data: &[u8]) -> Result<Signature, Error> {
249-
let mut ret = unsafe { ffi::Signature::blank() };
249+
let mut ret = ffi::Signature::new();
250250

251251
unsafe {
252252
if ffi::secp256k1_ecdsa_signature_parse_der(
@@ -265,7 +265,7 @@ impl Signature {
265265

266266
/// Converts a 64-byte compact-encoded byte slice to a signature
267267
pub fn from_compact(data: &[u8]) -> Result<Signature, Error> {
268-
let mut ret = unsafe { ffi::Signature::blank() };
268+
let mut ret = ffi::Signature::new();
269269
if data.len() != 64 {
270270
return Err(Error::InvalidSignature)
271271
}
@@ -290,7 +290,7 @@ impl Signature {
290290
/// support serializing to this "format"
291291
pub fn from_der_lax(data: &[u8]) -> Result<Signature, Error> {
292292
unsafe {
293-
let mut ret = ffi::Signature::blank();
293+
let mut ret = ffi::Signature::new();
294294
if ffi::ecdsa_signature_parse_der_lax(
295295
ffi::secp256k1_context_no_precomp,
296296
&mut ret,
@@ -605,7 +605,7 @@ impl<C: Signing> Secp256k1<C> {
605605
pub fn sign(&self, msg: &Message, sk: &key::SecretKey)
606606
-> Signature {
607607

608-
let mut ret = unsafe { ffi::Signature::blank() };
608+
let mut ret = ffi::Signature::new();
609609
unsafe {
610610
// We can assume the return value because it's not possible to construct
611611
// an invalid signature from a valid `Message` and `SecretKey`

src/macros.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,8 @@ macro_rules! impl_array_newtype {
6868
impl Clone for $thing {
6969
#[inline]
7070
fn clone(&self) -> $thing {
71-
unsafe {
72-
use core::intrinsics::copy_nonoverlapping;
73-
use core::mem;
74-
let mut ret: $thing = mem::uninitialized();
75-
copy_nonoverlapping(self.as_ptr(),
76-
ret.as_mut_ptr(),
77-
$len);
78-
ret
79-
}
71+
let &$thing(ref dat) = self;
72+
$thing(dat.clone())
8073
}
8174
}
8275

src/recovery/ffi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl RecoverableSignature {
2929
/// Create a new (zeroed) signature usable for the FFI interface
3030
pub fn new() -> RecoverableSignature { RecoverableSignature([0; 65]) }
3131
/// Create a new (uninitialized) signature usable for the FFI interface
32-
pub unsafe fn blank() -> RecoverableSignature { mem::uninitialized() }
32+
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
33+
pub unsafe fn blank() -> RecoverableSignature { RecoverableSignature::new() }
3334
}
3435

3536
impl Default for RecoverableSignature {

src/recovery/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl RecoverableSignature {
5757
/// representation is nonstandard and defined by the libsecp256k1
5858
/// library.
5959
pub fn from_compact(data: &[u8], recid: RecoveryId) -> Result<RecoverableSignature, Error> {
60-
let mut ret = unsafe { ffi::RecoverableSignature::blank() };
60+
let mut ret = ffi::RecoverableSignature::new();
6161

6262
unsafe {
6363
if data.len() != 64 {
@@ -103,7 +103,7 @@ impl RecoverableSignature {
103103
/// for verification
104104
#[inline]
105105
pub fn to_standard(&self) -> Signature {
106-
let mut ret = unsafe { super_ffi::Signature::blank() };
106+
let mut ret = super_ffi::Signature::new();
107107
unsafe {
108108
let err = ffi::secp256k1_ecdsa_recoverable_signature_convert(
109109
super_ffi::secp256k1_context_no_precomp,
@@ -130,7 +130,7 @@ impl<C: Signing> Secp256k1<C> {
130130
pub fn sign_recoverable(&self, msg: &Message, sk: &key::SecretKey)
131131
-> RecoverableSignature {
132132

133-
let mut ret = unsafe { ffi::RecoverableSignature::blank() };
133+
let mut ret = ffi::RecoverableSignature::new();
134134
unsafe {
135135
// We can assume the return value because it's not possible to construct
136136
// an invalid signature from a valid `Message` and `SecretKey`
@@ -157,7 +157,7 @@ impl<C: Verification> Secp256k1<C> {
157157
pub fn recover(&self, msg: &Message, sig: &RecoverableSignature)
158158
-> Result<key::PublicKey, Error> {
159159

160-
let mut pk = unsafe { super_ffi::PublicKey::blank() };
160+
let mut pk = super_ffi::PublicKey::new();
161161

162162
unsafe {
163163
if ffi::secp256k1_ecdsa_recover(self.ctx, &mut pk,

0 commit comments

Comments
 (0)