From e37f56dd0585e95fb0fdbf4345751ea4adeed87e Mon Sep 17 00:00:00 2001 From: George Pollard Date: Fri, 8 Jul 2022 13:24:11 +1200 Subject: [PATCH] Remove some allow(clippy::uninit_vec) (#2111) Closes #1595. Leaving one instance of `allow(clippy::uninit_vec)` in `pipe_handle.rs` as it is more difficult to remove. (Maybe there's another crate we could use for pipes?) To satisfy clippy, instead of creating the vec and then resizing it straight away (exposing uninitialized data), write into the `spare_capacity_mut()` (which is `&mut [MaybeUninit<_>]`) and then resize it afterwards. --- src/agent/debugger/src/breakpoint.rs | 10 +++++++--- src/agent/win-util/src/process.rs | 6 ++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/agent/debugger/src/breakpoint.rs b/src/agent/debugger/src/breakpoint.rs index 71cffa997..3182b7290 100644 --- a/src/agent/debugger/src/breakpoint.rs +++ b/src/agent/debugger/src/breakpoint.rs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -#![allow(clippy::uninit_vec)] - use std::{ collections::{btree_map::Range, BTreeMap}, ops::RangeBounds, @@ -269,10 +267,16 @@ impl BreakpointCollection { fn bulk_read_process_memory(&self, process_handle: HANDLE) -> Result> { let mut buffer: Vec = Vec::with_capacity(self.bulk_region_size()); + process::read_memory_array( + process_handle, + self.min_breakpoint_addr as _, + buffer.spare_capacity_mut(), + )?; + unsafe { buffer.set_len(self.bulk_region_size()); } - process::read_memory_array(process_handle, self.min_breakpoint_addr as _, &mut buffer)?; + Ok(buffer) } diff --git a/src/agent/win-util/src/process.rs b/src/agent/win-util/src/process.rs index fa95df13d..551840277 100644 --- a/src/agent/win-util/src/process.rs +++ b/src/agent/win-util/src/process.rs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -#![allow(clippy::uninit_vec)] - use std::{ ffi::OsString, mem::{size_of, MaybeUninit}, @@ -112,10 +110,10 @@ pub fn read_narrow_string( len: usize, ) -> Result { let mut buf: Vec = Vec::with_capacity(len); + read_memory_array(process_handle, remote_address, buf.spare_capacity_mut())?; unsafe { buf.set_len(len); } - read_memory_array::(process_handle, remote_address, &mut buf[..])?; Ok(String::from_utf8_lossy(&buf).into()) } @@ -125,10 +123,10 @@ pub fn read_wide_string( len: usize, ) -> Result { let mut buf: Vec = Vec::with_capacity(len); + read_memory_array(process_handle, remote_address, buf.spare_capacity_mut())?; unsafe { buf.set_len(len); } - read_memory_array::(process_handle, remote_address, &mut buf[..])?; Ok(OsString::from_wide(&buf)) }