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 <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2021-11-10 09:46:28 +01:00
parent ad521fd4e4
commit c8e3c1eed6
2 changed files with 10 additions and 13 deletions

View File

@ -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<MemoryRangeTable, MigratableError> {
assert!(length as usize % std::mem::size_of::<MemoryRange>() == 0);
let mut data = Vec::with_capacity(length as usize / (std::mem::size_of::<MemoryRange>()));
unsafe {
data.set_len(length as usize / (std::mem::size_of::<MemoryRange>()));
}
let mut data: Vec<MemoryRange> = Vec::new();
data.resize_with(
length as usize / (std::mem::size_of::<MemoryRange>()),
Default::default,
);
fd.read_exact(unsafe {
std::slice::from_raw_parts_mut(
data.as_ptr() as *mut MemoryRange as *mut u8,

View File

@ -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<u8> = 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<u8> = Vec::new();
data.resize_with(req.length() as usize, Default::default);
socket
.read_exact(&mut data)
.map_err(MigratableError::MigrateSocket)?;