From c8e3c1eed6d9774f42c34b65025f0b43f04e8986 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Wed, 10 Nov 2021 09:46:28 +0100 Subject: [PATCH] clippy: Make sure to initialize data Always properly initialize vectors so that we don't run in undefined behaviors when the vector gets dropped. Signed-off-by: Sebastien Boeuf --- vm-migration/src/protocol.rs | 11 ++++++----- vmm/src/lib.rs | 12 ++++-------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/vm-migration/src/protocol.rs b/vm-migration/src/protocol.rs index 2f17fc7ff..0d57d9c83 100644 --- a/vm-migration/src/protocol.rs +++ b/vm-migration/src/protocol.rs @@ -174,7 +174,7 @@ impl Response { unsafe impl ByteValued for Response {} #[repr(C)] -#[derive(Clone, Serialize, Deserialize, Versionize)] +#[derive(Clone, Default, Serialize, Deserialize, Versionize)] pub struct MemoryRange { pub gpa: u64, pub length: u64, @@ -227,10 +227,11 @@ impl MemoryRangeTable { pub fn read_from(fd: &mut dyn Read, length: u64) -> Result { assert!(length as usize % std::mem::size_of::() == 0); - let mut data = Vec::with_capacity(length as usize / (std::mem::size_of::())); - unsafe { - data.set_len(length as usize / (std::mem::size_of::())); - } + let mut data: Vec = Vec::new(); + data.resize_with( + length as usize / (std::mem::size_of::()), + Default::default, + ); fd.read_exact(unsafe { std::slice::from_raw_parts_mut( data.as_ptr() as *mut MemoryRange as *mut u8, diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index 5c917c8dd..50781a5f2 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -760,10 +760,8 @@ impl Vmm { T: Read + Write, { // Read in config data along with memory manager data - let mut data = Vec::with_capacity(req.length() as usize); - unsafe { - data.set_len(req.length() as usize); - } + let mut data: Vec = Vec::new(); + data.resize_with(req.length() as usize, Default::default); socket .read_exact(&mut data) .map_err(MigratableError::MigrateSocket)?; @@ -818,10 +816,8 @@ impl Vmm { T: Read + Write, { // Read in state data - let mut data = Vec::with_capacity(req.length() as usize); - unsafe { - data.set_len(req.length() as usize); - } + let mut data: Vec = Vec::new(); + data.resize_with(req.length() as usize, Default::default); socket .read_exact(&mut data) .map_err(MigratableError::MigrateSocket)?;