replace std::mem::uninitialized

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-08-21 16:49:12 +02:00
parent e59456e0b7
commit 45b5839e7e
2 changed files with 13 additions and 9 deletions

View File

@ -284,7 +284,7 @@ fn hexstr_to_ipv4addr<T: AsRef<[u8]>>(hex: T) -> Result<Ipv4Addr, Error> {
bail!("Error while converting hex string to IPv4 address: unexpected string length");
}
let mut addr: [u8; 4] = unsafe { std::mem::uninitialized() };
let mut addr = [0u8; 4];
for i in 0..4 {
addr[3 - i] = (hex_nibble(hex[i * 2])? << 4) + hex_nibble(hex[i * 2 + 1])?;
}
@ -344,10 +344,14 @@ fn hexstr_to_ipv6addr<T: AsRef<[u8]>>(hex: T) -> Result<Ipv6Addr, Error> {
bail!("Error while converting hex string to IPv6 address: unexpected string length");
}
let mut addr: [u8; 16] = unsafe { std::mem::uninitialized() };
for i in 0..16 {
addr[i] = (hex_nibble(hex[i * 2])? << 4) + hex_nibble(hex[i * 2 + 1])?;
}
let mut addr = std::mem::MaybeUninit::<[u8; 16]>::uninit();
let addr = unsafe {
let ap = &mut *addr.as_mut_ptr();
for i in 0..16 {
ap[i] = (hex_nibble(hex[i * 2])? << 4) + hex_nibble(hex[i * 2 + 1])?;
}
addr.assume_init()
};
Ok(Ipv6Addr::from(addr))
}
@ -367,7 +371,7 @@ fn hexstr_to_u32<T: AsRef<[u8]>>(hex: T) -> Result<u32, Error> {
bail!("Error while converting hex string to u32: unexpected string length");
}
let mut bytes: [u8; 4] = unsafe { std::mem::uninitialized() };
let mut bytes = [0u8; 4];
for i in 0..4 {
bytes[i] = (hex_nibble(hex[i * 2])? << 4) + hex_nibble(hex[i * 2 + 1])?;
}

View File

@ -201,12 +201,12 @@ impl<R: io::Read> ReadExt for R {
}
unsafe fn read_host_value<T: Endian>(&mut self) -> io::Result<T> {
let mut value: T = std::mem::uninitialized();
let mut value = std::mem::MaybeUninit::<T>::uninit();
self.read_exact(std::slice::from_raw_parts_mut(
&mut value as *mut T as *mut u8,
value.as_mut_ptr() as *mut u8,
std::mem::size_of::<T>(),
))?;
Ok(value)
Ok(value.assume_init())
}
unsafe fn read_le_value<T: Endian>(&mut self) -> io::Result<T> {