Skip to content

Commit f41a0ab

Browse files
committed
Replace mem::uninitialized with MaybeUninit
Closes #1503
1 parent 291f35e commit f41a0ab

File tree

3 files changed

+55
-67
lines changed

3 files changed

+55
-67
lines changed

openssl/src/aes.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
//! ```
5757
//!
5858
use libc::{c_int, c_uint};
59-
use std::{mem, ptr};
59+
use std::mem::MaybeUninit;
60+
use std::ptr;
6061

6162
use crate::symm::Mode;
6263

@@ -73,19 +74,18 @@ impl AesKey {
7374
/// # Failure
7475
///
7576
/// Returns an error if the key is not 128, 192, or 256 bits.
76-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
7777
pub fn new_encrypt(key: &[u8]) -> Result<AesKey, KeyError> {
7878
unsafe {
7979
assert!(key.len() <= c_int::max_value() as usize / 8);
8080

81-
let mut aes_key = mem::uninitialized();
81+
let mut aes_key = MaybeUninit::uninit();
8282
let r = ffi::AES_set_encrypt_key(
8383
key.as_ptr() as *const _,
8484
key.len() as c_int * 8,
85-
&mut aes_key,
85+
aes_key.as_mut_ptr(),
8686
);
8787
if r == 0 {
88-
Ok(AesKey(aes_key))
88+
Ok(AesKey(aes_key.assume_init()))
8989
} else {
9090
Err(KeyError(()))
9191
}
@@ -97,20 +97,19 @@ impl AesKey {
9797
/// # Failure
9898
///
9999
/// Returns an error if the key is not 128, 192, or 256 bits.
100-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
101100
pub fn new_decrypt(key: &[u8]) -> Result<AesKey, KeyError> {
102101
unsafe {
103102
assert!(key.len() <= c_int::max_value() as usize / 8);
104103

105-
let mut aes_key = mem::uninitialized();
104+
let mut aes_key = MaybeUninit::uninit();
106105
let r = ffi::AES_set_decrypt_key(
107106
key.as_ptr() as *const _,
108107
key.len() as c_int * 8,
109-
&mut aes_key,
108+
aes_key.as_mut_ptr(),
110109
);
111110

112111
if r == 0 {
113-
Ok(AesKey(aes_key))
112+
Ok(AesKey(aes_key.assume_init()))
114113
} else {
115114
Err(KeyError(()))
116115
}

openssl/src/ec.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ mod test {
11731173
}
11741174

11751175
#[test]
1176+
#[cfg(not(osslconf = "OPENSSL_NO_EC2M"))]
11761177
fn is_on_curve() {
11771178
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
11781179
let mut ctx = BigNumContext::new().unwrap();

openssl/src/sha.rs

+46-58
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
//! ```
3838
use cfg_if::cfg_if;
3939
use libc::c_void;
40-
use std::mem;
40+
use std::mem::MaybeUninit;
4141

4242
/// Computes the SHA1 hash of some data.
4343
///
@@ -46,23 +46,21 @@ use std::mem;
4646
/// SHA1 is known to be insecure - it should not be used unless required for
4747
/// compatibility with existing systems.
4848
#[inline]
49-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
5049
pub fn sha1(data: &[u8]) -> [u8; 20] {
5150
unsafe {
52-
let mut hash: [u8; 20] = mem::uninitialized();
53-
ffi::SHA1(data.as_ptr(), data.len(), hash.as_mut_ptr());
54-
hash
51+
let mut hash = MaybeUninit::<[u8; 20]>::uninit();
52+
ffi::SHA1(data.as_ptr(), data.len(), hash.as_mut_ptr().cast());
53+
hash.assume_init()
5554
}
5655
}
5756

5857
/// Computes the SHA224 hash of some data.
5958
#[inline]
60-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
6159
pub fn sha224(data: &[u8]) -> [u8; 28] {
6260
unsafe {
63-
let mut hash: [u8; 28] = mem::uninitialized();
64-
ffi::SHA224(data.as_ptr(), data.len(), hash.as_mut_ptr());
65-
hash
61+
let mut hash = MaybeUninit::<[u8; 28]>::uninit();
62+
ffi::SHA224(data.as_ptr(), data.len(), hash.as_mut_ptr().cast());
63+
hash.assume_init()
6664
}
6765
}
6866

@@ -71,9 +69,9 @@ pub fn sha224(data: &[u8]) -> [u8; 28] {
7169
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
7270
pub fn sha256(data: &[u8]) -> [u8; 32] {
7371
unsafe {
74-
let mut hash: [u8; 32] = mem::uninitialized();
75-
ffi::SHA256(data.as_ptr(), data.len(), hash.as_mut_ptr());
76-
hash
72+
let mut hash = MaybeUninit::<[u8; 32]>::uninit();
73+
ffi::SHA256(data.as_ptr(), data.len(), hash.as_mut_ptr().cast());
74+
hash.assume_init()
7775
}
7876
}
7977

@@ -82,9 +80,9 @@ pub fn sha256(data: &[u8]) -> [u8; 32] {
8280
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
8381
pub fn sha384(data: &[u8]) -> [u8; 48] {
8482
unsafe {
85-
let mut hash: [u8; 48] = mem::uninitialized();
86-
ffi::SHA384(data.as_ptr(), data.len(), hash.as_mut_ptr());
87-
hash
83+
let mut hash = MaybeUninit::<[u8; 48]>::uninit();
84+
ffi::SHA384(data.as_ptr(), data.len(), hash.as_mut_ptr().cast());
85+
hash.assume_init()
8886
}
8987
}
9088

@@ -93,9 +91,9 @@ pub fn sha384(data: &[u8]) -> [u8; 48] {
9391
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
9492
pub fn sha512(data: &[u8]) -> [u8; 64] {
9593
unsafe {
96-
let mut hash: [u8; 64] = mem::uninitialized();
97-
ffi::SHA512(data.as_ptr(), data.len(), hash.as_mut_ptr());
98-
hash
94+
let mut hash = MaybeUninit::<[u8; 64]>::uninit();
95+
ffi::SHA512(data.as_ptr(), data.len(), hash.as_mut_ptr().cast());
96+
hash.assume_init()
9997
}
10098
}
10199

@@ -120,12 +118,11 @@ cfg_if! {
120118
impl Sha1 {
121119
/// Creates a new hasher.
122120
#[inline]
123-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
124121
pub fn new() -> Sha1 {
125122
unsafe {
126-
let mut ctx = mem::uninitialized();
127-
ffi::SHA1_Init(&mut ctx);
128-
Sha1(ctx)
123+
let mut ctx = MaybeUninit::uninit();
124+
ffi::SHA1_Init( ctx.as_mut_ptr());
125+
Sha1(ctx.assume_init())
129126
}
130127
}
131128

@@ -141,12 +138,11 @@ cfg_if! {
141138

142139
/// Returns the hash of the data.
143140
#[inline]
144-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
145141
pub fn finish(mut self) -> [u8; 20] {
146142
unsafe {
147-
let mut hash: [u8; 20] = mem::uninitialized();
148-
ffi::SHA1_Final(hash.as_mut_ptr(), &mut self.0);
149-
hash
143+
let mut hash = MaybeUninit::<[u8; 20]>::uninit();
144+
ffi::SHA1_Final(hash.as_mut_ptr().cast(), &mut self.0);
145+
hash.assume_init()
150146
}
151147
}
152148
}
@@ -165,12 +161,11 @@ cfg_if! {
165161
impl Sha224 {
166162
/// Creates a new hasher.
167163
#[inline]
168-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
169164
pub fn new() -> Sha224 {
170165
unsafe {
171-
let mut ctx = mem::uninitialized();
172-
ffi::SHA224_Init(&mut ctx);
173-
Sha224(ctx)
166+
let mut ctx = MaybeUninit::uninit();
167+
ffi::SHA224_Init(ctx.as_mut_ptr());
168+
Sha224(ctx.assume_init())
174169
}
175170
}
176171

@@ -186,12 +181,11 @@ cfg_if! {
186181

187182
/// Returns the hash of the data.
188183
#[inline]
189-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
190184
pub fn finish(mut self) -> [u8; 28] {
191185
unsafe {
192-
let mut hash: [u8; 28] = mem::uninitialized();
193-
ffi::SHA224_Final(hash.as_mut_ptr(), &mut self.0);
194-
hash
186+
let mut hash = MaybeUninit::<[u8; 28]>::uninit();
187+
ffi::SHA224_Final(hash.as_mut_ptr().cast(), &mut self.0);
188+
hash.assume_init()
195189
}
196190
}
197191
}
@@ -210,12 +204,11 @@ cfg_if! {
210204
impl Sha256 {
211205
/// Creates a new hasher.
212206
#[inline]
213-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
214207
pub fn new() -> Sha256 {
215208
unsafe {
216-
let mut ctx = mem::uninitialized();
217-
ffi::SHA256_Init(&mut ctx);
218-
Sha256(ctx)
209+
let mut ctx = MaybeUninit::uninit();
210+
ffi::SHA256_Init(ctx.as_mut_ptr());
211+
Sha256(ctx.assume_init())
219212
}
220213
}
221214

@@ -231,12 +224,11 @@ cfg_if! {
231224

232225
/// Returns the hash of the data.
233226
#[inline]
234-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
235227
pub fn finish(mut self) -> [u8; 32] {
236228
unsafe {
237-
let mut hash: [u8; 32] = mem::uninitialized();
238-
ffi::SHA256_Final(hash.as_mut_ptr(), &mut self.0);
239-
hash
229+
let mut hash = MaybeUninit::<[u8; 32]>::uninit();
230+
ffi::SHA256_Final(hash.as_mut_ptr().cast(), &mut self.0);
231+
hash.assume_init()
240232
}
241233
}
242234
}
@@ -255,12 +247,11 @@ cfg_if! {
255247
impl Sha384 {
256248
/// Creates a new hasher.
257249
#[inline]
258-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
259250
pub fn new() -> Sha384 {
260251
unsafe {
261-
let mut ctx = mem::uninitialized();
262-
ffi::SHA384_Init(&mut ctx);
263-
Sha384(ctx)
252+
let mut ctx = MaybeUninit::uninit();
253+
ffi::SHA384_Init(ctx.as_mut_ptr());
254+
Sha384(ctx.assume_init())
264255
}
265256
}
266257

@@ -276,12 +267,11 @@ cfg_if! {
276267

277268
/// Returns the hash of the data.
278269
#[inline]
279-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
280270
pub fn finish(mut self) -> [u8; 48] {
281271
unsafe {
282-
let mut hash: [u8; 48] = mem::uninitialized();
283-
ffi::SHA384_Final(hash.as_mut_ptr(), &mut self.0);
284-
hash
272+
let mut hash = MaybeUninit::<[u8; 48]>::uninit();
273+
ffi::SHA384_Final(hash.as_mut_ptr().cast(), &mut self.0);
274+
hash.assume_init()
285275
}
286276
}
287277
}
@@ -300,12 +290,11 @@ cfg_if! {
300290
impl Sha512 {
301291
/// Creates a new hasher.
302292
#[inline]
303-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
304293
pub fn new() -> Sha512 {
305294
unsafe {
306-
let mut ctx = mem::uninitialized();
307-
ffi::SHA512_Init(&mut ctx);
308-
Sha512(ctx)
295+
let mut ctx = MaybeUninit::uninit();
296+
ffi::SHA512_Init(ctx.as_mut_ptr());
297+
Sha512(ctx.assume_init())
309298
}
310299
}
311300

@@ -321,12 +310,11 @@ cfg_if! {
321310

322311
/// Returns the hash of the data.
323312
#[inline]
324-
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
325313
pub fn finish(mut self) -> [u8; 64] {
326314
unsafe {
327-
let mut hash: [u8; 64] = mem::uninitialized();
328-
ffi::SHA512_Final(hash.as_mut_ptr(), &mut self.0);
329-
hash
315+
let mut hash= MaybeUninit::<[u8; 64]>::uninit();
316+
ffi::SHA512_Final(hash.as_mut_ptr().cast(), &mut self.0);
317+
hash.assume_init()
330318
}
331319
}
332320
}

0 commit comments

Comments
 (0)