From c947ac8ba217e3f7f1b35830e18bf6637e88e678 Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Fri, 3 Nov 2023 17:37:10 -0400 Subject: [PATCH] Review fixes part 4 plus more clippy lint fixes --- libclamav_rust/src/ctx.rs | 16 ++++++---------- libclamav_rust/src/evidence.rs | 6 +++--- libclamav_rust/src/fmap.rs | 21 ++++++++++++--------- libclamav_rust/src/fuzzy_hash.rs | 2 +- libclamav_rust/src/onenote.rs | 23 ++++++++++------------- libclamav_rust/src/scanners.rs | 20 +++++++++++--------- libclamav_rust/src/util.rs | 5 ++--- 7 files changed, 45 insertions(+), 48 deletions(-) diff --git a/libclamav_rust/src/ctx.rs b/libclamav_rust/src/ctx.rs index e988238241..275eaf0937 100644 --- a/libclamav_rust/src/ctx.rs +++ b/libclamav_rust/src/ctx.rs @@ -20,15 +20,11 @@ * MA 02110-1301, USA. */ -use std::convert::TryInto; -use std::path::{PathBuf}; -use std::slice; +use std::{convert::TryInto, path::PathBuf, slice}; use thiserror; -use crate::fmap::FMap; -use crate::sys::cli_ctx; -use crate::util::str_from_ptr; +use crate::{fmap::FMap, sys::cli_ctx, util::str_from_ptr}; /// Error enumerates all possible errors returned by this library. #[derive(thiserror::Error, Debug)] @@ -48,8 +44,8 @@ pub enum Error { #[error("Invalid FMap")] BadMap, - #[error("File path is not valid UTF8")] - FilepathNotUtf8, + #[error("String not UTF8: {0}")] + Utf8(#[from] std::str::Utf8Error), } /// Get the ctx.sub_filepath as an Option<'str> @@ -63,7 +59,7 @@ pub unsafe fn sub_filepath(ctx: *mut cli_ctx) -> Result, Error> } Ok(str_from_ptr(unsafe { *ctx }.sub_filepath) - .map_err(|_| Error::FilepathNotUtf8)? + .map_err(Error::Utf8)? .map(PathBuf::from)) } @@ -78,7 +74,7 @@ pub unsafe fn target_filepath(ctx: *mut cli_ctx) -> Result, Erro } Ok(str_from_ptr(unsafe { *ctx }.target_filepath) - .map_err(|_| Error::FilepathNotUtf8)? + .map_err(Error::Utf8)? .map(PathBuf::from)) } diff --git a/libclamav_rust/src/evidence.rs b/libclamav_rust/src/evidence.rs index f0e7404caa..088b59f262 100644 --- a/libclamav_rust/src/evidence.rs +++ b/libclamav_rust/src/evidence.rs @@ -247,14 +247,14 @@ impl Evidence { IndicatorType::Strong => { self.strong .entry(name.to_string()) - .or_insert_with(Vec::new) + .or_default() .push(meta); } IndicatorType::PotentiallyUnwanted => { self.pua .entry(name.to_string()) - .or_insert_with(Vec::new) + .or_default() .push(meta); } @@ -265,7 +265,7 @@ impl Evidence { IndicatorType::Weak => { self.weak .entry(name.to_string()) - .or_insert_with(Vec::new) + .or_default() .push(meta); } } diff --git a/libclamav_rust/src/fmap.rs b/libclamav_rust/src/fmap.rs index 380d1a7a39..1188e291cc 100644 --- a/libclamav_rust/src/fmap.rs +++ b/libclamav_rust/src/fmap.rs @@ -23,13 +23,13 @@ use std::convert::TryFrom; use log::{debug, error}; -use thiserror::Error; +use thiserror; use crate::{sys, util::str_from_ptr}; -/// FMapError enumerates all possible errors returned by this library. -#[derive(Error, Debug)] -pub enum FMapError { +/// Error enumerates all possible errors returned by this library. +#[derive(thiserror::Error, Debug)] +pub enum Error { #[error("Invalid parameter: {0}")] InvalidParameter(String), @@ -41,6 +41,9 @@ pub enum FMapError { #[error("FMap pointer not initialized: {0}")] UninitializedPtr(&'static str), + + #[error("Attempted to create Rust FMap interface from NULL pointer")] + Null, } #[derive(PartialEq, Eq, Hash, Debug)] @@ -49,11 +52,11 @@ pub struct FMap { } impl TryFrom<*mut sys::cl_fmap_t> for FMap { - type Error = &'static str; + type Error = Error; fn try_from(value: *mut sys::cl_fmap_t) -> Result { if value.is_null() { - return Err::("Attempted to create Rust FMap interface from NULL pointer"); + return Err(Error::Null); } Ok(FMap { fmap_ptr: value }) @@ -62,11 +65,11 @@ impl TryFrom<*mut sys::cl_fmap_t> for FMap { impl<'a> FMap { /// Simple wrapper around C FMAP module's fmap.need() method. - pub fn need_off(&'a self, at: usize, len: usize) -> Result<&'a [u8], FMapError> { + pub fn need_off(&'a self, at: usize, len: usize) -> Result<&'a [u8], Error> { // Get the need() method function pointer from the fmap. let need_fn = match unsafe { *self.fmap_ptr }.need { Some(ptr) => ptr, - None => return Err(FMapError::UninitializedPtr("need()")), + None => return Err(Error::UninitializedPtr("need()")), }; let ptr: *const u8 = unsafe { need_fn(self.fmap_ptr, at, len, 1) } as *const u8; @@ -77,7 +80,7 @@ impl<'a> FMap { "need_off at {:?} len {:?} for fmap size {:?} returned NULL", at, len, fmap_size ); - return Err(FMapError::NotContained(at, len, fmap_size)); + return Err(Error::NotContained(at, len, fmap_size)); } let slice: &[u8] = unsafe { std::slice::from_raw_parts(ptr, len) }; diff --git a/libclamav_rust/src/fuzzy_hash.rs b/libclamav_rust/src/fuzzy_hash.rs index 79bbef9f8f..67eb14592d 100644 --- a/libclamav_rust/src/fuzzy_hash.rs +++ b/libclamav_rust/src/fuzzy_hash.rs @@ -308,7 +308,7 @@ impl FuzzyHashMap { // Then add the current meta struct to the entry. self.hashmap .entry(fuzzy_hash) - .or_insert_with(Vec::new) + .or_default() .push(meta); Ok(()) diff --git a/libclamav_rust/src/onenote.rs b/libclamav_rust/src/onenote.rs index e1187760b5..245c04d979 100644 --- a/libclamav_rust/src/onenote.rs +++ b/libclamav_rust/src/onenote.rs @@ -20,10 +20,10 @@ * MA 02110-1301, USA. */ -use std::mem; -use std::panic; -use std::path::Path; -use std::path::PathBuf; +use std::{ + mem, panic, + path::{Path, PathBuf}, +}; use log::{debug, error}; use thiserror; @@ -125,15 +125,12 @@ impl<'a> OneNote<'a> { let name = embedded_file.filename(); // If name is empty, set to None. - let name = match name { - "" => { - debug!("Found unnamed attached file of size {}-bytes", data.len()); - None - }, - _ => { - debug!("Found attached file '{}' of size {}-bytes", name, data.len()); - Some(name.to_string()) - }, + let name = if name.is_empty() { + debug!("Found unnamed attached file of size {}-bytes", data.len()); + None + } else { + debug!("Found attached file '{}' of size {}-bytes", name, data.len()); + Some(name.to_string()) }; embedded_files.push(ExtractedFile { diff --git a/libclamav_rust/src/scanners.rs b/libclamav_rust/src/scanners.rs index 07dc42444e..84631852aa 100644 --- a/libclamav_rust/src/scanners.rs +++ b/libclamav_rust/src/scanners.rs @@ -20,17 +20,19 @@ * MA 02110-1301, USA. */ -use std::ffi::{c_char, CString}; -use std::path::Path; -use std::ptr::null_mut; +use std::{ + ffi::{c_char, CString}, + path::Path, + ptr::null_mut, +}; use libc::c_void; use log::{debug, error, warn}; -use crate::ctx; -use crate::onenote::OneNote; -use crate::sys::{ - cl_error_t, cl_error_t_CL_ERROR, cl_error_t_CL_SUCCESS, cli_ctx, cli_magic_scan_buff, +use crate::{ + ctx, + onenote::OneNote, + sys::{cl_error_t, cl_error_t_CL_ERROR, cl_error_t_CL_SUCCESS, cli_ctx, cli_magic_scan_buff}, }; /// Rust wrapper of libclamav's cli_magic_scan_buff() function. @@ -72,9 +74,9 @@ fn magic_scan(ctx: *mut cli_ctx, buf: &[u8], name: Option) -> cl_error_t } /// Scan a OneNote file for attachments -/// +/// /// # Safety -/// +/// /// Must be a valid ctx pointer. #[no_mangle] pub unsafe extern "C" fn scan_onenote(ctx: *mut cli_ctx) -> cl_error_t { diff --git a/libclamav_rust/src/util.rs b/libclamav_rust/src/util.rs index 9b8fb0c457..47f2d8b4b3 100644 --- a/libclamav_rust/src/util.rs +++ b/libclamav_rust/src/util.rs @@ -20,8 +20,7 @@ * MA 02110-1301, USA. */ -use std::ffi::CStr; -use std::fs::File; +use std::{ffi::CStr, fs::File}; /// Obtain a std::fs::File from an i32 in a platform-independent manner. /// @@ -54,7 +53,7 @@ pub fn file_from_fd_or_handle(fd: i32) -> File { /// /// The caller is responsible for making sure the lifetime of the pointer /// exceeds the lifetime of the output string. -/// +/// /// ptr must be a valid pointer to a C string. pub unsafe fn str_from_ptr(ptr: *const i8) -> Result, std::str::Utf8Error> { if ptr.is_null() {