Skip to content

Commit

Permalink
Fix clippy lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
micahsnyder committed Dec 14, 2024
1 parent acae138 commit 4c78958
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 151 deletions.
2 changes: 1 addition & 1 deletion libclamav_rust/src/alz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl AlzLocalFileHeader {
data: buffer.to_vec(),
};

if 0 != extracted_file.data.len() {
if !extracted_file.data.is_empty() {
files.push(extracted_file);
}
}
Expand Down
56 changes: 21 additions & 35 deletions libclamav_rust/src/cdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,7 @@ fn cmd_move(ctx: &mut Context, move_op: MoveOp) -> Result<(), InputError> {
.map_err(|e| {
InputError::ProcessingString(format!(
"Failed to open destination file {:?} for MOVE command: {}",
&move_op.dst,
e.to_string()
&move_op.dst, e
))
})?;

Expand All @@ -801,7 +800,7 @@ fn cmd_move(ctx: &mut Context, move_op: MoveOp) -> Result<(), InputError> {
InputError::ProcessingString(format!(
"Failed to create temp file in current directory {:?} for MOVE command: {}",
std::env::current_dir(),
e.to_string()
e
))
})?;
let mut tmp_file = tmp_named_file.as_file();
Expand All @@ -810,8 +809,7 @@ fn cmd_move(ctx: &mut Context, move_op: MoveOp) -> Result<(), InputError> {
let mut src_reader = BufReader::new(File::open(&move_op.src).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to open source file {:?}: {} for MOVE command",
&move_op.src,
e.to_string()
&move_op.src, e
))
})?);

Expand All @@ -824,8 +822,7 @@ fn cmd_move(ctx: &mut Context, move_op: MoveOp) -> Result<(), InputError> {
let n_read = src_reader.read_until(b'\n', &mut line).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to read from source file {:?} for MOVE command: {}",
&move_op.src,
e.to_string()
&move_op.src, e
))
})?;
if n_read == 0 {
Expand Down Expand Up @@ -860,7 +857,7 @@ fn cmd_move(ctx: &mut Context, move_op: MoveOp) -> Result<(), InputError> {
InputError::ProcessingString(format!(
"Failed to write line to temp file {:?} for MOVE command: {}",
tmp_named_file.path(),
e.to_string()
e
))
})?;
}
Expand All @@ -880,16 +877,15 @@ fn cmd_move(ctx: &mut Context, move_op: MoveOp) -> Result<(), InputError> {
fs::remove_file(&move_op.src).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to remove original source file {:?} for MOVE command: {}",
&move_op.src,
e.to_string()
&move_op.src, e
))
})?;
fs::rename(tmp_named_file.path(), &move_op.src).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to rename temp file {:?} to {:?} for MOVE command: {}",
tmp_named_file.path(),
&move_op.src,
e.to_string()
e
))
})?;

Expand All @@ -911,8 +907,7 @@ fn cmd_close(ctx: &mut Context) -> Result<(), InputError> {
let mut src_reader = BufReader::new(File::open(&open_db).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to open db file {:?} for CLOSE command: {}",
&open_db,
e.to_string()
&open_db, e
))
})?);

Expand All @@ -924,7 +919,7 @@ fn cmd_close(ctx: &mut Context) -> Result<(), InputError> {
InputError::ProcessingString(format!(
"Failed to create temp file in current directory {:?} for CLOSE command: {}",
std::env::current_dir(),
e.to_string()
e
))
})?;
let tmp_file = tmp_named_file.as_file();
Expand All @@ -937,7 +932,7 @@ fn cmd_close(ctx: &mut Context) -> Result<(), InputError> {
InputError::ProcessingString(format!(
"Failed to read temp file {:?} for CLOSE command: {}",
tmp_named_file.path(),
e.to_string()
e
))
})?;
if n_read == 0 {
Expand Down Expand Up @@ -989,14 +984,14 @@ fn cmd_close(ctx: &mut Context) -> Result<(), InputError> {
InputError::ProcessingString(format!(
"Failed to write line to temp file {:?} for CLOSE command: {}",
tmp_named_file.path(),
e.to_string()
e
))
})?;
tmp_file.write_all(b"\n").map_err(|e| {
InputError::ProcessingString(format!(
"Failed to write new line to temp file {:?} for CLOSE command: {}",
tmp_named_file.path(),
e.to_string()
e
))
})?;
}
Expand Down Expand Up @@ -1035,32 +1030,26 @@ fn cmd_close(ctx: &mut Context) -> Result<(), InputError> {
fs::remove_file(tmpfile_path).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to remove the temp file file {:?} for CLOSE command: {}",
tmpfile_path,
e.to_string()
tmpfile_path, e
))
})?;
return Err(InputError::ProcessingString(format!(
"Failed to remove open db file {:?} for CLOSE command: {}",
&open_db,
e.to_string()
&open_db, e
))
.into());
}
if let Err(e) = fs::rename(&tmpfile_path, &open_db) {
fs::remove_file(&tmpfile_path).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to remove temp file {:?}: {} for CLOSE command",
&tmpfile_path,
e.to_string()
&tmpfile_path, e
))
})?;
return Err(InputError::ProcessingString(format!(
"Failed to rename temp file {:?} to {:?} for CLOSE command: {}",
tmpfile_path,
&open_db,
e.to_string()
))
.into());
tmpfile_path, &open_db, e
)));
}
}

Expand All @@ -1073,15 +1062,13 @@ fn cmd_close(ctx: &mut Context) -> Result<(), InputError> {
.map_err(|e| {
InputError::ProcessingString(format!(
"Failed to open db file {:?} for CLOSE command: {}",
open_db,
e.to_string()
open_db, e
))
})?;
db_file.write_all(&ctx.additions).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to write add lines to db {:?} for CLOSE command: {}",
open_db,
e.to_string()
open_db, e
))
})?;
ctx.additions.clear();
Expand All @@ -1104,8 +1091,7 @@ fn cmd_unlink(ctx: &mut Context, unlink_op: UnlinkOp) -> Result<(), InputError>
fs::remove_file(unlink_op.db_name).map_err(|e| {
InputError::ProcessingString(format!(
"Failed to remove db file {:?} for UNLINK command: {}",
unlink_op.db_name,
e.to_string()
unlink_op.db_name, e
))
})?;

Expand All @@ -1117,7 +1103,7 @@ fn process_line(ctx: &mut Context, line: &[u8]) -> Result<(), InputError> {
let mut tokens = line.splitn(2, |b| *b == b' ' || *b == b'\n');
let cmd = tokens.next().ok_or(InputError::MissingCommand)?;
let remainder_with_nl = tokens.next();
let remainder = remainder_with_nl.and_then(|s| s.strip_suffix(&[b'\n']));
let remainder = remainder_with_nl.and_then(|s| s.strip_suffix(b"\n"));

// Call the appropriate command function
match cmd {
Expand Down
31 changes: 15 additions & 16 deletions libclamav_rust/src/codesign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub unsafe extern "C" fn codesign_sign_file(
std::slice::from_raw_parts(intermediate_cert_paths_str, intermediate_cert_paths_len);
// now convert the intermediate_cert_path_strs to a Vec<&Path>
let intermediate_cert_paths: Vec<PathBuf> = intermediate_cert_path_strs
.into_iter()
.iter()
.filter_map(|&path_str| -> Option<PathBuf> {
let path_str = if path_str.is_null() {
warn!("Intermiediate path string is NULL");
Expand All @@ -148,7 +148,7 @@ pub unsafe extern "C" fn codesign_sign_file(
"Invalid intermediate certificate path: '{}' {}",
path_str, e
);
return None;
None
}
}
} else {
Expand All @@ -173,7 +173,7 @@ pub unsafe extern "C" fn codesign_sign_file(

match sign_file(
&target_file_path,
&signature_file_path,
signature_file_path,
&signing_cert_path,
&intermediate_cert_paths,
&signing_key_path,
Expand All @@ -183,7 +183,7 @@ pub unsafe extern "C" fn codesign_sign_file(
true
}
Err(e) => {
return ffi_error!(err = err, e);
ffi_error!(err = err, e)
}
}
}
Expand Down Expand Up @@ -220,21 +220,21 @@ where
// Seek to the end of the file
writer.seek(std::io::SeekFrom::End(0))?;
// Write a newline before the signature
writer.write(b"\n")?;
writer.write_all(b"\n")?;

writer
} else {
// Create the signature file if it doesn't exist.
let mut writer = std::io::BufWriter::new(std::fs::File::create(signature_file_path)?);
writer.write(b"#clamsign-1.0\n")?;
writer.write_all(b"#clamsign-1.0\n")?;

writer
};

writer.write(sig_bytes.as_bytes())?;
writer.write_all(sig_bytes.as_bytes())?;

// Write a newline after the signature
writer.write(b"\n")?;
writer.write_all(b"\n")?;

Ok(())
}
Expand Down Expand Up @@ -316,7 +316,7 @@ pub unsafe extern "C" fn codesign_verify_file(
true
}
Err(e) => {
return ffi_error!(err = err, e);
ffi_error!(err = err, e)
}
}
}
Expand All @@ -330,9 +330,9 @@ pub fn verify_signed_file(
signature_file_path: &Path,
certs_directory: &Path,
) -> Result<String, Error> {
let signature_file: File = File::open(&signature_file_path)?;
let signature_file: File = File::open(signature_file_path)?;

let mut signed_file: File = File::open(&signed_file_path)?;
let mut signed_file: File = File::open(signed_file_path)?;

let mut file_data = Vec::<u8>::new();
let read_result = signed_file.read_to_end(&mut file_data);
Expand Down Expand Up @@ -447,9 +447,9 @@ pub fn verify_signed_file(
};
}

return Err(Error::CannotVerify(
Err(Error::CannotVerify(
"Unable to verify any digital signatures".to_string(),
));
))
}

pub struct Signer {
Expand Down Expand Up @@ -491,8 +491,7 @@ impl Signer {
pub fn sign(&self, data: &[u8]) -> Result<Pkcs7, Error> {
let flags = Pkcs7Flags::DETACHED | Pkcs7Flags::BINARY;

Pkcs7::sign(&self.cert, &self.key, &self.certs, data, flags)
.map_err(|e| Error::OpenSSLError(e))
Pkcs7::sign(&self.cert, &self.key, &self.certs, data, flags).map_err(Error::OpenSSLError)
}
}

Expand Down Expand Up @@ -522,7 +521,7 @@ impl Verifier {
// That doesn't mean the signature is invalid, only that we don't have the required public key to verify it.
for cert in cert_stack {
let serial = cert.serial_number().to_bn()?;
if top_level_signer == None {
if top_level_signer.is_none() {
let signer = cert
.subject_name()
.entries()
Expand Down
Loading

0 comments on commit 4c78958

Please sign in to comment.