From 2491f79ff9e77652ebdfc4b7689cdf77eaae93aa Mon Sep 17 00:00:00 2001 From: Costin Lupu Date: Mon, 8 Jan 2024 18:52:10 +0100 Subject: [PATCH] Fix errors reported by Clippy (part 2 - manual) Warnings that weren't fixed automatically by the previous are fixed by this commit. Signed-off-by: Costin Lupu --- driver-bindings/src/lib.rs | 2 +- src/common/mod.rs | 2 +- src/enclave_proc/connection_listener.rs | 2 +- src/enclave_proc/resource_manager.rs | 8 ++++---- tests/test_dev_driver.rs | 19 ++++++++++++------- vsock_proxy/src/starter.rs | 2 +- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/driver-bindings/src/lib.rs b/driver-bindings/src/lib.rs index e7f0f903..ee1a33d7 100644 --- a/driver-bindings/src/lib.rs +++ b/driver-bindings/src/lib.rs @@ -7,5 +7,5 @@ #![allow(missing_docs)] #![allow(non_camel_case_types)] -mod bindings; +pub mod bindings; pub use self::bindings::*; diff --git a/src/common/mod.rs b/src/common/mod.rs index c4d7caa1..fafc37d2 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -572,7 +572,7 @@ mod tests { const TMP_DIR_STR: &str = "./tmp_sock_dir"; - fn unset_envvar(varname: &String) { + fn unset_envvar(varname: &str) { unsafe { libc::unsetenv(varname.as_ptr() as *const c_char); }; diff --git a/src/enclave_proc/connection_listener.rs b/src/enclave_proc/connection_listener.rs index 173b63c8..b611f1ae 100644 --- a/src/enclave_proc/connection_listener.rs +++ b/src/enclave_proc/connection_listener.rs @@ -320,7 +320,7 @@ mod tests { const THREADS_STR: &str = "Threads:"; const TMP_DIR: &str = "./npe"; - fn unset_envvar(varname: &String) { + fn unset_envvar(varname: &str) { unsafe { libc::unsetenv(varname.as_ptr() as *const c_char) }; } diff --git a/src/enclave_proc/resource_manager.rs b/src/enclave_proc/resource_manager.rs index 5454d134..600061ce 100644 --- a/src/enclave_proc/resource_manager.rs +++ b/src/enclave_proc/resource_manager.rs @@ -368,7 +368,7 @@ impl ResourceAllocator { // Always allocate larger pages first, to reduce fragmentation and page count. // Once an allocation of a given page size fails, proceed to the next smaller // page size and retry. - for (_, page_info) in HUGE_PAGE_MAP.iter().enumerate() { + for page_info in HUGE_PAGE_MAP.iter() { while needed_mem >= page_info.1 as i64 { match MemoryRegion::new(page_info.0) { Ok(value) => { @@ -389,7 +389,7 @@ impl ResourceAllocator { // need to allocate in increasing order of page size in order to reduce wastage). if needed_mem > 0 { - for (_, page_info) in HUGE_PAGE_MAP.iter().rev().enumerate() { + for page_info in HUGE_PAGE_MAP.iter().rev() { while needed_mem > 0 { match MemoryRegion::new(page_info.0) { Ok(value) => { @@ -420,7 +420,7 @@ impl ResourceAllocator { .sort_by(|reg1, reg2| reg2.mem_size.cmp(®1.mem_size)); needed_mem = self.requested_mem as i64; - for (_, region) in self.mem_regions.iter().enumerate() { + for region in self.mem_regions.iter() { if needed_mem <= 0 { break; } @@ -434,7 +434,7 @@ impl ResourceAllocator { self.mem_regions.drain(split_index..); // Generate a summary of the allocated memory. - for (_, region) in self.mem_regions.iter().enumerate() { + for region in self.mem_regions.iter() { if let Some(page_count) = allocated_pages.get_mut(®ion.mem_size) { *page_count += 1; } else { diff --git a/tests/test_dev_driver.rs b/tests/test_dev_driver.rs index d9c0307e..f0ec7c5b 100644 --- a/tests/test_dev_driver.rs +++ b/tests/test_dev_driver.rs @@ -7,6 +7,7 @@ use std::fs::File; use std::os::unix::io::{AsRawFd, RawFd}; use std::process::Command; +use driver_bindings::bindings::ne_enclave_start_info; use nitro_cli::common::{NitroCliErrorEnum, NitroCliFailure, NitroCliResult}; use nitro_cli::enclave_proc::cpu_info::CpuInfo; use nitro_cli::enclave_proc::resource_manager::{ @@ -201,12 +202,12 @@ impl CheckDmesg { ]; let lines = self.get_dmesg_lines().unwrap(); - for i in self.recorded_line..lines.len() { - let upper_line = lines[i].to_uppercase(); + for line in lines.iter().skip(self.recorded_line) { + let upper_line = line.to_uppercase(); for word in checks.iter() { if upper_line.contains(&word.to_uppercase()) { return Err(NitroCliFailure::new() - .add_subaction(format!("Dmesg line: {} contains: {}", lines[i], word)) + .add_subaction(format!("Dmesg line: {} contains: {}", line, word)) .set_error_code(NitroCliErrorEnum::IoctlFailure) .set_file_and_line(file!(), line!())); } @@ -536,14 +537,18 @@ mod test_dev_driver { assert!(result.is_ok()); // Start with an invalid flag. - let mut enclave_start_info = EnclaveStartInfo::default(); - enclave_start_info.flags = 1234; + let enclave_start_info = ne_enclave_start_info { + flags: 1234, + ..Default::default() + }; let result = enclave.start(enclave_start_info); assert!(result.is_err()); // Start with an invalid CID. - let mut enclave_start_info = EnclaveStartInfo::default(); - enclave_start_info.enclave_cid = VMADDR_CID_LOCAL as u64; + let mut enclave_start_info = ne_enclave_start_info { + enclave_cid: VMADDR_CID_LOCAL as u64, + ..Default::default() + }; let result = enclave.start(enclave_start_info); assert!(result.is_err()); diff --git a/vsock_proxy/src/starter.rs b/vsock_proxy/src/starter.rs index 7684425e..ed78c326 100644 --- a/vsock_proxy/src/starter.rs +++ b/vsock_proxy/src/starter.rs @@ -48,7 +48,7 @@ pub fn check_allowlist( // Obtain the remote server's IP address. let mut addrs = Proxy::parse_addr(remote_host, only_4, only_6) .map_err(|err| format!("Could not parse remote address: {}", err))?; - let remote_addr = *addrs.get(0).ok_or("No IP address found")?; + let remote_addr = *addrs.first().ok_or("No IP address found")?; for raw_service in services { let addr = raw_service["address"].as_str().ok_or("No address field")?;