Skip to content

Commit f5a40b9

Browse files
committed
construct less err
1 parent 18c8097 commit f5a40b9

File tree

9 files changed

+21
-18
lines changed

9 files changed

+21
-18
lines changed

crates/libafl/src/corpus/cached.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
fn cache_testcase_input<'a>(&'a self, testcase: &'a mut Testcase<I>) -> Result<(), Error> {
3333
let id = testcase
3434
.corpus_id()
35-
.ok_or(Error::unknown("The testcase is not associated with an id"))?;
35+
.ok_or_else(|| Error::unknown("The testcase is not associated with an id"))?;
3636
if testcase.input().is_none() {
3737
self.inner.load_input_into(testcase)?;
3838
let mut borrowed_num = 0;

crates/libafl/src/feedbacks/stdio.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ impl StdOutToMetadataFeedback {
4343
{
4444
let observer = observers
4545
.get(&self.o_ref)
46-
.ok_or(Error::illegal_state("StdOutObserver is missing"))?;
46+
.ok_or_else(|| Error::illegal_state("StdOutObserver is missing"))?;
4747
let buffer = observer
4848
.output
4949
.as_ref()
50-
.ok_or(Error::illegal_state("StdOutObserver has no stdout"))?;
50+
.ok_or_else(|| Error::illegal_state("StdOutObserver has no stdout"))?;
5151
let stdout = String::from_utf8_lossy(buffer).into_owned();
5252

5353
testcase
@@ -136,11 +136,11 @@ where
136136
) -> Result<(), Error> {
137137
let observer = observers
138138
.get(&self.o_ref)
139-
.ok_or(Error::illegal_state("StdErrObserver is missing"))?;
139+
.ok_or_else(|| Error::illegal_state("StdErrObserver is missing"))?;
140140
let buffer = observer
141141
.output
142142
.as_ref()
143-
.ok_or(Error::illegal_state("StdErrObserver has no stderr"))?;
143+
.ok_or_else(|| Error::illegal_state("StdErrObserver has no stderr"))?;
144144
let stderr = String::from_utf8_lossy(buffer).into_owned();
145145

146146
testcase

crates/libafl/src/schedulers/weighted.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,11 @@ where
238238

239239
/// Cycles the strategy of the scheduler; tries to mimic AFL++'s cycling formula
240240
fn cycle_schedule(&mut self, metadata: &mut SchedulerMetadata) -> Result<(), Error> {
241-
let mut ps = metadata.strat().ok_or(Error::illegal_argument(
242-
"No strategy specified when initializing scheduler; cannot cycle!",
243-
))?;
241+
let mut ps = metadata.strat().ok_or_else(|| {
242+
Error::illegal_argument(
243+
"No strategy specified when initializing scheduler; cannot cycle!",
244+
)
245+
})?;
244246
let new_base = match ps.base() {
245247
BaseSchedule::EXPLORE => BaseSchedule::EXPLOIT,
246248
BaseSchedule::COE => BaseSchedule::LIN,

crates/libafl_nyx/src/helper.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ impl NyxHelper {
3636
where
3737
P: AsRef<Path>,
3838
{
39-
let share_dir_str = share_dir.as_ref().to_str().ok_or(Error::illegal_argument(
40-
"`share_dir` contains invalid UTF-8",
41-
))?;
39+
let share_dir_str = share_dir
40+
.as_ref()
41+
.to_str()
42+
.ok_or_else(|| Error::illegal_argument("`share_dir` contains invalid UTF-8"))?;
4243

4344
let mut nyx_config = NyxConfig::load(share_dir_str).map_err(|e| {
4445
Error::illegal_argument(format!("Failed to load Nyx config from share dir: {e}"))

fuzzers/binary_only/intel_pt_command_executor/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6060
executable_segments
6161
.into_iter()
6262
.next()
63-
.ok_or(Error::illegal_argument(
63+
.ok_or_else(||Error::illegal_argument(
6464
"No executable segment found in target program",
6565
))?;
6666
log::debug!(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn set_solution_filepath(
8484
fn parse_time_line(line: &str) -> Result<u64, Error> {
8585
line.split(": ")
8686
.last()
87-
.ok_or(Error::illegal_state("invalid stats file"))?
87+
.ok_or_else(||Error::illegal_state("invalid stats file"))?
8888
.parse()
8989
.map_err(|_| Error::illegal_state("invalid stats file"))
9090
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ fn parse_target_env(s: &str) -> Result<Option<HashMap<String, String>>, Error> {
137137
for vars in env_regex.captures_iter(s) {
138138
_ = target_env.insert(
139139
vars.get(1)
140-
.ok_or(Error::illegal_argument("invalid AFL_TARGET_ENV format"))?
140+
.ok_or_else(||Error::illegal_argument("invalid AFL_TARGET_ENV format"))?
141141
.as_str()
142142
.to_string(),
143143
vars.get(2)
144-
.ok_or(Error::illegal_argument("invalid AFL_TARGET_ENV format"))?
144+
.ok_or_else(||Error::illegal_argument("invalid AFL_TARGET_ENV format"))?
145145
.as_str()
146146
.to_string(),
147147
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn find_afl_binary(filename: &str, same_dir_as: Option<PathBuf>) -> Result<P
235235
if !is_library {
236236
// finally, check the path for the binary
237237
return find_executable_in_path(&filename)
238-
.ok_or(Error::unknown(format!("cannot find {filename}")));
238+
.ok_or_else(||Error::unknown(format!("cannot find {filename}")));
239239
}
240240

241241
Err(Error::unknown(format!("cannot find {filename}")))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,12 @@ define_run_client!(state, mgr, fuzzer_dir, core_id, opt, is_main_node, {
392392
state.walk_initial_inputs(std::slice::from_ref(&opt.input_dir), |path: &PathBuf| {
393393
let mut filename = path
394394
.file_name()
395-
.ok_or(Error::illegal_state(format!(
395+
.ok_or_else(||Error::illegal_state(format!(
396396
"file {} in input directory does not have a filename",
397397
path.display()
398398
)))?
399399
.to_str()
400-
.ok_or(Error::illegal_state(format!(
400+
.ok_or_else(||Error::illegal_state(format!(
401401
"file {} in input directory does not have a legal filename",
402402
path.display()
403403
)))?

0 commit comments

Comments
 (0)