From 9f092a6f310f328c0aa30fbd74bb9d64ab15d67d Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Wed, 14 Feb 2018 16:44:02 +0000 Subject: [PATCH] remove all use of mem::uninitialized and mem::copy_nonoverlapping --- src/internal_macros.rs | 31 +++++++++++-------------------- src/network/address.rs | 12 ++++-------- src/util/hash.rs | 6 +++--- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/internal_macros.rs b/src/internal_macros.rs index 69018a4f..c159b303 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -82,15 +82,9 @@ macro_rules! impl_array_newtype { impl<'a> From<&'a [$ty]> for $thing { fn from(data: &'a [$ty]) -> $thing { assert_eq!(data.len(), $len); - unsafe { - use std::intrinsics::copy_nonoverlapping; - use std::mem; - let mut ret: $thing = mem::uninitialized(); - copy_nonoverlapping(data.as_ptr(), - ret.as_mut_ptr(), - $len); - ret - } + let mut ret = [0; $len]; + ret.copy_from_slice(&data[..]); + $thing(ret) } } @@ -193,18 +187,15 @@ macro_rules! impl_array_newtype_encodable { fn visit_seq(&mut self, mut v: V) -> Result<$thing, V::Error> where V: ::serde::de::SeqVisitor { - unsafe { - use std::mem; - let mut ret: [$ty; $len] = mem::uninitialized(); - for item in ret.iter_mut() { - *item = match try!(v.visit()) { - Some(c) => c, - None => return Err(::serde::de::Error::end_of_stream()) - }; - } - try!(v.end()); - Ok($thing(ret)) + let mut ret: [$ty; $len] = [0; $len]; + for item in ret.iter_mut() { + *item = match try!(v.visit()) { + Some(c) => c, + None => return Err(::serde::de::Error::end_of_stream()) + }; } + try!(v.end()); + Ok($thing(ret)) } } diff --git a/src/network/address.rs b/src/network/address.rs index 6ac93735..0d9e18d6 100644 --- a/src/network/address.rs +++ b/src/network/address.rs @@ -68,14 +68,10 @@ impl fmt::Debug for Address { impl Clone for Address { fn clone(&self) -> Address { - unsafe { - use std::intrinsics::copy_nonoverlapping; - use std::mem; - let mut ret = mem::uninitialized(); - copy_nonoverlapping(self, - &mut ret, - mem::size_of::
()); - ret + Address { + services: self.services, + address: self.address, + port: self.port, } } } diff --git a/src/util/hash.rs b/src/util/hash.rs index cf6748f9..39783d7c 100644 --- a/src/util/hash.rs +++ b/src/util/hash.rs @@ -255,7 +255,7 @@ impl Sha256dHash { } let bytes = s.as_bytes(); - let mut ret: [u8; 32] = unsafe { mem::uninitialized() }; + let mut ret = [0; 32]; for i in 0..32 { let hi = match bytes[2*i] { b @ b'0'...b'9' => (b - b'0') as u8, @@ -314,9 +314,9 @@ impl serde::Serialize for Sha256dHash { where S: serde::Serializer, { unsafe { - use std::{char, mem, str}; + use std::{char, str}; - let mut string: [u8; 64] = mem::uninitialized(); + let mut string = [0; 64]; for i in 0..32 { string[2 * i] = char::from_digit((self.0[31 - i] / 0x10) as u32, 16).unwrap() as u8; string[2 * i + 1] = char::from_digit((self.0[31 - i] & 0x0f) as u32, 16).unwrap() as u8;