Skip to content

Commit

Permalink
Review fixes part 4 plus more clippy lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
micahsnyder committed Nov 3, 2023
1 parent 38ec0ad commit c947ac8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 48 deletions.
16 changes: 6 additions & 10 deletions libclamav_rust/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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>
Expand All @@ -63,7 +59,7 @@ pub unsafe fn sub_filepath(ctx: *mut cli_ctx) -> Result<Option<PathBuf>, Error>
}

Ok(str_from_ptr(unsafe { *ctx }.sub_filepath)
.map_err(|_| Error::FilepathNotUtf8)?
.map_err(Error::Utf8)?
.map(PathBuf::from))
}

Expand All @@ -78,7 +74,7 @@ pub unsafe fn target_filepath(ctx: *mut cli_ctx) -> Result<Option<PathBuf>, Erro
}

Ok(str_from_ptr(unsafe { *ctx }.target_filepath)
.map_err(|_| Error::FilepathNotUtf8)?
.map_err(Error::Utf8)?
.map(PathBuf::from))
}

Expand Down
6 changes: 3 additions & 3 deletions libclamav_rust/src/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -265,7 +265,7 @@ impl Evidence {
IndicatorType::Weak => {
self.weak
.entry(name.to_string())
.or_insert_with(Vec::new)
.or_default()
.push(meta);
}
}
Expand Down
21 changes: 12 additions & 9 deletions libclamav_rust/src/fmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand All @@ -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)]
Expand All @@ -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<Self, Self::Error> {
if value.is_null() {
return Err::<Self, &str>("Attempted to create Rust FMap interface from NULL pointer");
return Err(Error::Null);
}

Ok(FMap { fmap_ptr: value })
Expand All @@ -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;
Expand All @@ -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) };
Expand Down
2 changes: 1 addition & 1 deletion libclamav_rust/src/fuzzy_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
23 changes: 10 additions & 13 deletions libclamav_rust/src/onenote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions libclamav_rust/src/scanners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -72,9 +74,9 @@ fn magic_scan(ctx: *mut cli_ctx, buf: &[u8], name: Option<String>) -> 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 {
Expand Down
5 changes: 2 additions & 3 deletions libclamav_rust/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<Option<&'static str>, std::str::Utf8Error> {
if ptr.is_null() {
Expand Down

0 comments on commit c947ac8

Please sign in to comment.