Skip to content

Commit d9e89b8

Browse files
authored
Cleanup path parameters (#3526)
* Cleanup all paths * fix * Cargo update
1 parent 8f4ced7 commit d9e89b8

File tree

10 files changed

+423
-292
lines changed

10 files changed

+423
-292
lines changed

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/libafl_bolts/src/fs.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! `LibAFL` functionality for filesystem interaction
22
3-
use alloc::{borrow::ToOwned, string::String, sync::Arc, vec::Vec};
3+
use alloc::{string::String, sync::Arc, vec::Vec};
44
use core::{
55
sync::atomic::{AtomicU64, Ordering},
66
time::Duration,
@@ -44,23 +44,21 @@ pub fn write_file_atomic<P>(path: P, bytes: &[u8]) -> Result<(), Error>
4444
where
4545
P: AsRef<Path>,
4646
{
47-
fn inner(path: &Path, bytes: &[u8]) -> Result<(), Error> {
48-
let mut tmpfile_name = path.to_path_buf();
49-
tmpfile_name.set_file_name(format!(
50-
".{}.tmp",
51-
tmpfile_name.file_name().unwrap().to_string_lossy()
52-
));
53-
54-
let mut tmpfile = OpenOptions::new()
55-
.write(true)
56-
.create_new(true)
57-
.open(&tmpfile_name)?;
58-
59-
tmpfile.write_all(bytes)?;
60-
fs::rename(&tmpfile_name, path)?;
61-
Ok(())
62-
}
63-
inner(path.as_ref(), bytes)
47+
let path = path.as_ref();
48+
let mut tmpfile_name = path.to_path_buf();
49+
tmpfile_name.set_file_name(format!(
50+
".{}.tmp",
51+
tmpfile_name.file_name().unwrap().to_string_lossy()
52+
));
53+
54+
let mut tmpfile = OpenOptions::new()
55+
.write(true)
56+
.create_new(true)
57+
.open(&tmpfile_name)?;
58+
59+
tmpfile.write_all(bytes)?;
60+
fs::rename(&tmpfile_name, path)?;
61+
Ok(())
6462
}
6563

6664
/// An [`InputFile`] to write fuzzer input to.
@@ -99,16 +97,17 @@ impl InputFile {
9997
/// Creates a new [`InputFile`], or truncates if it already exists
10098
pub fn create<P>(filename: P) -> Result<Self, Error>
10199
where
102-
P: AsRef<Path>,
100+
P: Into<PathBuf>,
103101
{
102+
let filename = filename.into();
104103
let f = OpenOptions::new()
105104
.create(true)
106105
.read(true)
107106
.write(true)
108107
.truncate(true)
109108
.open(&filename)?;
110109
Ok(Self {
111-
path: filename.as_ref().to_owned(),
110+
path: filename,
112111
file: f,
113112
rc: Arc::new(()),
114113
})
@@ -145,11 +144,11 @@ impl InputFile {
145144
/// This method works recursively.
146145
/// If `last` is `None`, it'll load all file.
147146
pub fn find_new_files_rec<P: AsRef<Path>>(
148-
dir: P,
147+
dir_path: P,
149148
last_check: &Option<Duration>,
150149
) -> Result<Vec<PathBuf>, Error> {
151150
let mut new_files = Vec::<PathBuf>::new();
152-
for entry in fs::read_dir(dir)? {
151+
for entry in fs::read_dir(dir_path)? {
153152
let entry = entry?;
154153
let path = entry.path();
155154
let attributes = fs::metadata(&path);

crates/libafl_bolts/src/target_args.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use alloc::{borrow::ToOwned, vec::Vec};
44
use std::{
55
ffi::{OsStr, OsString},
6-
path::Path,
6+
path::PathBuf,
77
};
88

99
use crate::fs::{InputFile, get_unique_std_input_file};
@@ -120,14 +120,14 @@ pub trait StdTargetArgs: Sized {
120120
/// Note: If you use this, you should ensure that there is only one instance using this
121121
/// file at any given time.
122122
#[must_use]
123-
fn arg_input_file<P: AsRef<Path>>(self, path: P) -> Self {
124-
let mut moved = self.arg(path.as_ref());
123+
fn arg_input_file<P: Into<PathBuf>>(self, path: P) -> Self {
124+
let path = path.into();
125+
let mut moved = self.arg(&path);
125126
assert!(
126127
match &moved.inner().input_location {
127-
InputLocation::File { out_file } => out_file.path.as_path() == path.as_ref(),
128-
InputLocation::StdIn { input_file } => input_file
129-
.as_ref()
130-
.is_none_or(|of| of.path.as_path() == path.as_ref()),
128+
InputLocation::File { out_file } => out_file.path == path,
129+
InputLocation::StdIn { input_file } =>
130+
input_file.as_ref().is_none_or(|of| of.path == path),
131131
InputLocation::Arg { argnum: _ } => false,
132132
},
133133
"Already specified an input file under a different name. This is not supported"

crates/libafl_frida/src/drcov_rt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Generates `DrCov` traces
22
use alloc::rc::Rc;
33
use core::hash::{BuildHasher, Hasher};
4-
use std::path::{Path, PathBuf};
4+
use std::path::PathBuf;
55

66
use ahash::RandomState;
77
use frida_gum::ModuleMap;
@@ -78,9 +78,9 @@ impl DrCovRuntime {
7878
}
7979

8080
/// Create a new [`DrCovRuntime`] that writes coverage to the specified directory
81-
pub fn with_path<P: AsRef<Path>>(path: P) -> Self {
81+
pub fn with_path<P: Into<PathBuf>>(path: P) -> Self {
8282
Self {
83-
coverage_directory: path.as_ref().into(),
83+
coverage_directory: path.into(),
8484
..Self::default()
8585
}
8686
}

crates/libafl_qemu/src/qemu/config.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::{
22
fmt,
33
fmt::{Display, Formatter},
44
};
5-
use std::path::{Path, PathBuf};
5+
use std::path::PathBuf;
66

77
use getset::Getters;
88
use libafl_derive;
@@ -228,11 +228,9 @@ impl Display for Bios {
228228
}
229229

230230
#[cfg(feature = "systemmode")]
231-
impl<R: AsRef<Path>> From<R> for Bios {
232-
fn from(path: R) -> Self {
233-
Self {
234-
path: path.as_ref().to_path_buf(),
235-
}
231+
impl<P: Into<Path>> From<P> for Bios {
232+
fn from(path: P) -> Self {
233+
Self { path: path.into() }
236234
}
237235
}
238236

@@ -250,11 +248,9 @@ impl Display for Kernel {
250248
}
251249

252250
#[cfg(feature = "systemmode")]
253-
impl<R: AsRef<Path>> From<R> for Kernel {
254-
fn from(path: R) -> Self {
255-
Self {
256-
path: path.as_ref().to_path_buf(),
257-
}
251+
impl<P: Into<PathBuf>> From<P> for Kernel {
252+
fn from(path: P) -> Self {
253+
Self { path: path.into() }
258254
}
259255
}
260256

@@ -294,11 +290,9 @@ impl Display for InitRD {
294290
}
295291

296292
#[cfg(feature = "systemmode")]
297-
impl<R: AsRef<Path>> From<R> for InitRD {
298-
fn from(path: R) -> Self {
299-
Self {
300-
path: path.as_ref().to_path_buf(),
301-
}
293+
impl<P: Into<PathBuf>> From<P> for InitRD {
294+
fn from(path: P) -> Self {
295+
Self { path: path.into() }
302296
}
303297
}
304298

@@ -313,11 +307,9 @@ impl Display for LoadVM {
313307
}
314308
}
315309

316-
impl<R: AsRef<Path>> From<R> for LoadVM {
317-
fn from(path: R) -> Self {
318-
Self {
319-
path: path.as_ref().to_path_buf(),
320-
}
310+
impl<P: Into<PathBuf>> From<P> for LoadVM {
311+
fn from(path: P) -> Self {
312+
Self { path: path.into() }
321313
}
322314
}
323315

@@ -492,11 +484,9 @@ impl Display for Program {
492484
}
493485

494486
#[cfg(feature = "usermode")]
495-
impl<R: AsRef<Path>> From<R> for Program {
496-
fn from(path: R) -> Self {
497-
Self {
498-
path: path.as_ref().to_path_buf(),
499-
}
487+
impl<P: Into<PathBuf>> From<P> for Program {
488+
fn from(path: P) -> Self {
489+
Self { path: path.into() }
500490
}
501491
}
502492

crates/libafl_targets/src/drcov.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ fn parse_path(s: &str) -> PathBuf {
272272

273273
impl DrCovReader {
274274
/// Parse a `drcov` file to memory.
275-
pub fn read<P: AsRef<Path> + ?Sized>(file: &P) -> Result<Self, Error> {
276-
let f = File::open(file)?;
275+
pub fn read<P: AsRef<Path> + ?Sized>(path: &P) -> Result<Self, Error> {
276+
let f = File::open(path.as_ref())?;
277277
let mut reader = BufReader::new(f);
278278

279279
let mut header = String::new();

crates/shmem_providers/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,10 @@ pub mod unix_shmem {
780780
/// This will *NOT* automatically delete the shmem files, meaning that it's user's responsibility to delete them after fuzzing
781781
pub fn new(
782782
map_size: usize,
783-
filename: impl AsRef<Path>,
783+
path: impl AsRef<Path>,
784784
use_fd_as_id: bool,
785785
) -> Result<Self, Error> {
786-
let filename_bytes = filename.as_ref().as_os_str().as_encoded_bytes();
786+
let filename_bytes = path.as_ref().as_os_str().as_encoded_bytes();
787787

788788
let mut filename_path: [u8; 20] = [0_u8; MAX_MMAP_FILENAME_LEN];
789789
// Keep room for the leading slash and trailing NULL.
@@ -1038,9 +1038,9 @@ pub mod unix_shmem {
10381038
pub fn new_shmem_with_id(
10391039
&mut self,
10401040
map_size: usize,
1041-
id: impl AsRef<Path>,
1041+
id_path: impl AsRef<Path>,
10421042
) -> Result<MmapShMem, Error> {
1043-
MmapShMem::new(map_size, id, self.use_fd_as_id)
1043+
MmapShMem::new(map_size, id_path, self.use_fd_as_id)
10441044
}
10451045

10461046
/// Create a new [`MmapShMemProvider`] where filename is used as the shmem id.

fuzzers/forkserver/libafl-fuzz/src/executor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ fn is_instrumented(mmap: &Mmap, shmem_env_var: &str) -> bool {
179179
mmap_has_substr(mmap, shmem_env_var)
180180
}
181181

182-
fn find_executable_in_path<P: AsRef<Path>>(executable: &P) -> Option<PathBuf> {
182+
fn find_executable_in_path<P: AsRef<Path>>(executable_path: &P) -> Option<PathBuf> {
183183
std::env::var_os("PATH").and_then(|paths| {
184184
std::env::split_paths(&paths).find_map(|dir| {
185-
let full_path = dir.join(executable);
185+
let full_path = dir.join(executable_path);
186186
if full_path.is_file() {
187187
Some(full_path)
188188
} else {

0 commit comments

Comments
 (0)