Commit f41a0abb authored by Steven Fackler's avatar Steven Fackler
Browse files

Replace mem::uninitialized with MaybeUninit

Closes #1503
parent 291f35e1
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -56,7 +56,8 @@
//! ```
//!
use libc::{c_int, c_uint};
use std::{mem, ptr};
use std::mem::MaybeUninit;
use std::ptr;

use crate::symm::Mode;

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

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

            let mut aes_key = mem::uninitialized();
            let mut aes_key = MaybeUninit::uninit();
            let r = ffi::AES_set_decrypt_key(
                key.as_ptr() as *const _,
                key.len() as c_int * 8,
                &mut aes_key,
                aes_key.as_mut_ptr(),
            );

            if r == 0 {
                Ok(AesKey(aes_key))
                Ok(AesKey(aes_key.assume_init()))
            } else {
                Err(KeyError(()))
            }
+1 −0
Original line number Diff line number Diff line
@@ -1173,6 +1173,7 @@ mod test {
    }

    #[test]
    #[cfg(not(osslconf = "OPENSSL_NO_EC2M"))]
    fn is_on_curve() {
        let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
        let mut ctx = BigNumContext::new().unwrap();
+46 −58
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@
//! ```
use cfg_if::cfg_if;
use libc::c_void;
use std::mem;
use std::mem::MaybeUninit;

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

/// Computes the SHA224 hash of some data.
#[inline]
#[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
pub fn sha224(data: &[u8]) -> [u8; 28] {
    unsafe {
        let mut hash: [u8; 28] = mem::uninitialized();
        ffi::SHA224(data.as_ptr(), data.len(), hash.as_mut_ptr());
        hash
        let mut hash = MaybeUninit::<[u8; 28]>::uninit();
        ffi::SHA224(data.as_ptr(), data.len(), hash.as_mut_ptr().cast());
        hash.assume_init()
    }
}

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

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

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

@@ -120,12 +118,11 @@ cfg_if! {
        impl Sha1 {
            /// Creates a new hasher.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn new() -> Sha1 {
                unsafe {
                    let mut ctx = mem::uninitialized();
                    ffi::SHA1_Init(&mut ctx);
                    Sha1(ctx)
                    let mut ctx = MaybeUninit::uninit();
                    ffi::SHA1_Init( ctx.as_mut_ptr());
                    Sha1(ctx.assume_init())
                }
            }

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

            /// Returns the hash of the data.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn finish(mut self) -> [u8; 20] {
                unsafe {
                    let mut hash: [u8; 20] = mem::uninitialized();
                    ffi::SHA1_Final(hash.as_mut_ptr(), &mut self.0);
                    hash
                    let mut hash = MaybeUninit::<[u8; 20]>::uninit();
                    ffi::SHA1_Final(hash.as_mut_ptr().cast(), &mut self.0);
                    hash.assume_init()
                }
            }
        }
@@ -165,12 +161,11 @@ cfg_if! {
        impl Sha224 {
            /// Creates a new hasher.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn new() -> Sha224 {
                unsafe {
                    let mut ctx = mem::uninitialized();
                    ffi::SHA224_Init(&mut ctx);
                    Sha224(ctx)
                    let mut ctx = MaybeUninit::uninit();
                    ffi::SHA224_Init(ctx.as_mut_ptr());
                    Sha224(ctx.assume_init())
                }
            }

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

            /// Returns the hash of the data.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn finish(mut self) -> [u8; 28] {
                unsafe {
                    let mut hash: [u8; 28] = mem::uninitialized();
                    ffi::SHA224_Final(hash.as_mut_ptr(), &mut self.0);
                    hash
                    let mut hash = MaybeUninit::<[u8; 28]>::uninit();
                    ffi::SHA224_Final(hash.as_mut_ptr().cast(), &mut self.0);
                    hash.assume_init()
                }
            }
        }
@@ -210,12 +204,11 @@ cfg_if! {
        impl Sha256 {
            /// Creates a new hasher.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn new() -> Sha256 {
                unsafe {
                    let mut ctx = mem::uninitialized();
                    ffi::SHA256_Init(&mut ctx);
                    Sha256(ctx)
                    let mut ctx = MaybeUninit::uninit();
                    ffi::SHA256_Init(ctx.as_mut_ptr());
                    Sha256(ctx.assume_init())
                }
            }

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

            /// Returns the hash of the data.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn finish(mut self) -> [u8; 32] {
                unsafe {
                    let mut hash: [u8; 32] = mem::uninitialized();
                    ffi::SHA256_Final(hash.as_mut_ptr(), &mut self.0);
                    hash
                    let mut hash = MaybeUninit::<[u8; 32]>::uninit();
                    ffi::SHA256_Final(hash.as_mut_ptr().cast(), &mut self.0);
                    hash.assume_init()
                }
            }
        }
@@ -255,12 +247,11 @@ cfg_if! {
        impl Sha384 {
            /// Creates a new hasher.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn new() -> Sha384 {
                unsafe {
                    let mut ctx = mem::uninitialized();
                    ffi::SHA384_Init(&mut ctx);
                    Sha384(ctx)
                    let mut ctx = MaybeUninit::uninit();
                    ffi::SHA384_Init(ctx.as_mut_ptr());
                    Sha384(ctx.assume_init())
                }
            }

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

            /// Returns the hash of the data.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn finish(mut self) -> [u8; 48] {
                unsafe {
                    let mut hash: [u8; 48] = mem::uninitialized();
                    ffi::SHA384_Final(hash.as_mut_ptr(), &mut self.0);
                    hash
                    let mut hash = MaybeUninit::<[u8; 48]>::uninit();
                    ffi::SHA384_Final(hash.as_mut_ptr().cast(), &mut self.0);
                    hash.assume_init()
                }
            }
        }
@@ -300,12 +290,11 @@ cfg_if! {
        impl Sha512 {
            /// Creates a new hasher.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn new() -> Sha512 {
                unsafe {
                    let mut ctx = mem::uninitialized();
                    ffi::SHA512_Init(&mut ctx);
                    Sha512(ctx)
                    let mut ctx = MaybeUninit::uninit();
                    ffi::SHA512_Init(ctx.as_mut_ptr());
                    Sha512(ctx.assume_init())
                }
            }

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

            /// Returns the hash of the data.
            #[inline]
            #[allow(deprecated)] // https://github.com/rust-lang/rust/issues/63566
            pub fn finish(mut self) -> [u8; 64] {
                unsafe {
                    let mut hash: [u8; 64] = mem::uninitialized();
                ffi::SHA512_Final(hash.as_mut_ptr(), &mut self.0);
                hash
                    let mut hash= MaybeUninit::<[u8; 64]>::uninit();
                ffi::SHA512_Final(hash.as_mut_ptr().cast(), &mut self.0);
                hash.assume_init()
            }
        }
    }