diff --git a/libafl/src/bolts/serdeany.rs b/libafl/src/bolts/serdeany.rs index 1d0062b36f..2ed0f77359 100644 --- a/libafl/src/bolts/serdeany.rs +++ b/libafl/src/bolts/serdeany.rs @@ -71,7 +71,6 @@ macro_rules! create_serde_registry_for_trait { pub mod $mod_name { use alloc::boxed::Box; - use alloc::string::String; use core::any::{Any, TypeId}; use core::fmt; use postcard; @@ -260,7 +259,7 @@ macro_rules! create_serde_registry_for_trait { impl NamedSerdeAnyMap { #[inline] - pub fn get(&self, name: &String) -> Option<&T> + pub fn get(&self, name: &str) -> Option<&T> where T: Any, { @@ -273,11 +272,7 @@ macro_rules! create_serde_registry_for_trait { } #[inline] - pub fn by_typeid( - &self, - name: &String, - typeid: &TypeId, - ) -> Option<&dyn $trait_name> { + pub fn by_typeid(&self, name: &str, typeid: &TypeId) -> Option<&dyn $trait_name> { match self.map.get(&unpack_type_id(*typeid)) { None => None, Some(h) => h @@ -287,7 +282,7 @@ macro_rules! create_serde_registry_for_trait { } #[inline] - pub fn get_mut(&mut self, name: &String) -> Option<&mut T> + pub fn get_mut(&mut self, name: &str) -> Option<&mut T> where T: Any, { @@ -302,7 +297,7 @@ macro_rules! create_serde_registry_for_trait { #[inline] pub fn by_typeid_mut( &mut self, - name: &String, + name: &str, typeid: &TypeId, ) -> Option<&mut dyn $trait_name> { match self.map.get_mut(&unpack_type_id(*typeid)) { @@ -423,7 +418,7 @@ macro_rules! create_serde_registry_for_trait { } #[inline] - pub fn insert(&mut self, val: Box, name: &String) { + pub fn insert(&mut self, val: Box, name: &str) { let id = unpack_type_id((*val).type_id()); if !self.map.contains_key(&id) { self.map.insert(id, HashMap::default()); @@ -448,7 +443,7 @@ macro_rules! create_serde_registry_for_trait { } #[inline] - pub fn contains(&self, name: &String) -> bool + pub fn contains(&self, name: &str) -> bool where T: Any, { diff --git a/libafl/src/bolts/shmem.rs b/libafl/src/bolts/shmem.rs index f6edf33ff8..6ddb7cbfed 100644 --- a/libafl/src/bolts/shmem.rs +++ b/libafl/src/bolts/shmem.rs @@ -71,7 +71,7 @@ pub trait ShMem: Sized + Debug { fn description(&self) -> ShMemDescription { ShMemDescription { size: self.map().len(), - str_bytes: self.shm_slice().clone(), + str_bytes: *self.shm_slice(), } } @@ -103,7 +103,7 @@ pub trait ShMem: Sized + Debug { #[cfg(feature = "std")] pub mod shmem { - use core::{mem::size_of, slice}; + use core::{mem::size_of, ptr, slice}; use libc::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, c_void}; use std::ffi::CStr; @@ -222,7 +222,7 @@ pub mod shmem { pub fn from_str(shm_str: &CStr, map_size: usize) -> Result { let mut ret = afl_shmem_unitialized(); let map = unsafe { afl_shmem_by_str(&mut ret, shm_str, map_size) }; - if map != 0 as *mut u8 { + if !map.is_null() { Ok(ret) } else { Err(Error::Unknown(format!( @@ -235,7 +235,7 @@ pub mod shmem { pub fn new(map_size: usize) -> Result { let mut ret = afl_shmem_unitialized(); let map = unsafe { afl_shmem_init(&mut ret, map_size) }; - if map != 0 as *mut u8 { + if !map.is_null() { Ok(ret) } else { Err(Error::Unknown(format!( @@ -253,24 +253,24 @@ pub mod shmem { // Not set or not initialized; return; } - (*shm).shm_str[0 as usize] = '\u{0}' as u8; - shmctl((*shm).shm_id, 0 as c_int, 0 as *mut shmid_ds); - (*shm).map = 0 as *mut c_uchar; + (*shm).shm_str[0 as usize] = 0u8; + shmctl((*shm).shm_id, 0 as c_int, ptr::null_mut()); + (*shm).map = ptr::null_mut(); } /// Functions to create Shared memory region, for observation channels and /// opening inputs and stuff. unsafe fn afl_shmem_init(shm: *mut UnixShMem, map_size: usize) -> *mut c_uchar { (*shm).map_size = map_size; - (*shm).map = 0 as *mut c_uchar; + (*shm).map = ptr::null_mut(); (*shm).shm_id = shmget( 0 as c_int, map_size as c_ulong, 0o1000 as c_int | 0o2000 as c_int | 0o600 as c_int, ); if (*shm).shm_id < 0 as c_int { - (*shm).shm_str[0] = '\u{0}' as u8; - return 0 as *mut c_uchar; + (*shm).shm_str[0] = 0u8; + return ptr::null_mut(); } snprintf( (*shm).shm_str.as_mut_ptr() as *mut c_char, @@ -280,13 +280,13 @@ pub mod shmem { ); (*shm).shm_str [(size_of::<[c_char; 20]>() as c_ulong).wrapping_sub(1 as c_int as c_ulong) as usize] = - '\u{0}' as u8; - (*shm).map = shmat((*shm).shm_id, 0 as *const c_void, 0 as c_int) as *mut c_uchar; + 0u8; + (*shm).map = shmat((*shm).shm_id, ptr::null(), 0 as c_int) as *mut c_uchar; if (*shm).map == -(1 as c_int) as *mut c_void as *mut c_uchar || (*shm).map.is_null() { - shmctl((*shm).shm_id, 0 as c_int, 0 as *mut shmid_ds); + shmctl((*shm).shm_id, 0 as c_int, ptr::null_mut()); (*shm).shm_id = -(1 as c_int); - (*shm).shm_str[0 as c_int as usize] = '\u{0}' as u8; - return 0 as *mut c_uchar; + (*shm).shm_str[0 as c_int as usize] = 0u8; + return ptr::null_mut(); } return (*shm).map; } @@ -297,10 +297,10 @@ pub mod shmem { shm_str: &CStr, map_size: usize, ) -> *mut c_uchar { - if shm.is_null() || shm_str.to_bytes().len() == 0 || map_size == 0 { - return 0 as *mut c_uchar; + if shm.is_null() || shm_str.to_bytes().is_empty() || map_size == 0 { + return ptr::null_mut(); } - (*shm).map = 0 as *mut c_uchar; + (*shm).map = ptr::null_mut(); (*shm).map_size = map_size; strncpy( (*shm).shm_str.as_mut_ptr() as *mut c_char, @@ -312,12 +312,12 @@ pub mod shmem { .expect(&format!("illegal shm_str {:?}", shm_str)) .parse::() .unwrap(); - (*shm).map = shmat((*shm).shm_id, 0 as *const c_void, 0 as c_int) as *mut c_uchar; + (*shm).map = shmat((*shm).shm_id, ptr::null(), 0 as c_int) as *mut c_uchar; if (*shm).map == -(1 as c_int) as *mut c_void as *mut c_uchar { - (*shm).map = 0 as *mut c_uchar; + (*shm).map = ptr::null_mut(); (*shm).map_size = 0; - (*shm).shm_str[0] = '\u{0}' as u8; - return 0 as *mut c_uchar; + (*shm).shm_str[0] = 0u8; + return ptr::null_mut(); } return (*shm).map; } diff --git a/libafl/src/bolts/tuples.rs b/libafl/src/bolts/tuples.rs index 162bea12ce..21d93442c7 100644 --- a/libafl/src/bolts/tuples.rs +++ b/libafl/src/bolts/tuples.rs @@ -6,6 +6,9 @@ use core::any::TypeId; pub trait HasLen { fn len(&self) -> usize; + fn is_empty(&self) -> bool { + self.len() == 0 + } } impl HasLen for () { diff --git a/libafl/src/corpus/minimizer.rs b/libafl/src/corpus/minimizer.rs index 42c37d9d84..a57dc0d4cd 100644 --- a/libafl/src/corpus/minimizer.rs +++ b/libafl/src/corpus/minimizer.rs @@ -37,6 +37,12 @@ impl TopRatedsMetadata { } } +impl Default for TopRatedsMetadata { + fn default() -> Self { + Self::new() + } +} + pub trait FavFactor where I: Input, diff --git a/libafl/src/corpus/mod.rs b/libafl/src/corpus/mod.rs index 6b2fc082d5..7e9baa3a53 100644 --- a/libafl/src/corpus/mod.rs +++ b/libafl/src/corpus/mod.rs @@ -128,6 +128,7 @@ where I: Input, R: Rand, { + /// Create a new RandCorpusScheduler that just schedules randomly. pub fn new() -> Self { Self { phantom: PhantomData, @@ -135,4 +136,16 @@ where } } +impl Default for RandCorpusScheduler +where + S: HasCorpus + HasRand, + C: Corpus, + I: Input, + R: Rand, +{ + fn default() -> Self { + Self::new() + } +} + pub type StdCorpusScheduler = RandCorpusScheduler; diff --git a/libafl/src/corpus/ondisk.rs b/libafl/src/corpus/ondisk.rs index f597d45bdd..e0787d6816 100644 --- a/libafl/src/corpus/ondisk.rs +++ b/libafl/src/corpus/ondisk.rs @@ -35,18 +35,15 @@ where /// Add an entry to the corpus and return its index #[inline] fn add(&mut self, mut testcase: Testcase) -> Result { - match testcase.filename() { - None => { - // TODO walk entry metadata to ask for pices of filename (e.g. :havoc in AFL) - let filename = self.dir_path.join(format!("id_{}", &self.entries.len())); - let filename_str = filename.to_str().expect("Invalid Path"); - testcase.set_filename(filename_str.into()); - } - _ => {} - } + if let None = testcase.filename() { + // TODO walk entry metadata to ask for pices of filename (e.g. :havoc in AFL) + let filename = self.dir_path.join(format!("id_{}", &self.entries.len())); + let filename_str = filename.to_str().expect("Invalid Path"); + testcase.set_filename(filename_str.into()); + }; testcase .store_input() - .expect("Could not save testcase to disk".into()); + .expect("Could not save testcase to disk"); self.entries.push(RefCell::new(testcase)); Ok(self.entries.len() - 1) } diff --git a/libafl/src/corpus/queue.rs b/libafl/src/corpus/queue.rs index 5257a52d8e..3bd3727ff8 100644 --- a/libafl/src/corpus/queue.rs +++ b/libafl/src/corpus/queue.rs @@ -59,6 +59,17 @@ where } } +impl Default for QueueCorpusScheduler +where + S: HasCorpus, + C: Corpus, + I: Input, +{ + fn default() -> Self { + Self::new() + } +} + /* #[cfg(test)] #[cfg(feature = "std")] diff --git a/libafl/src/corpus/testcase.rs b/libafl/src/corpus/testcase.rs index 625e005ca2..d84f9835d9 100644 --- a/libafl/src/corpus/testcase.rs +++ b/libafl/src/corpus/testcase.rs @@ -181,7 +181,7 @@ where #[inline] pub fn with_fitness(input: I, fitness: u32) -> Self { Testcase { - input: Some(input.into()), + input: Some(input), filename: None, fitness: fitness, metadata: SerdeAnyMap::new(), diff --git a/libafl/src/events/llmp.rs b/libafl/src/events/llmp.rs index 102cab12d9..b47f70cb8b 100644 --- a/libafl/src/events/llmp.rs +++ b/libafl/src/events/llmp.rs @@ -167,10 +167,7 @@ where /// Returns if we are the broker pub fn is_broker(&self) -> bool { - match self.llmp { - llmp::LlmpConnection::IsBroker { broker: _ } => true, - _ => false, - } + matches!(self.llmp, llmp::LlmpConnection::IsBroker { broker: _ }) } /// Run forever in the broker @@ -284,9 +281,9 @@ where // TODO include ExitKind in NewTestcase let fitness = state.is_interesting(&input, &observers, ExitKind::Ok)?; if fitness > 0 { - if !state + if state .add_if_interesting(&input, fitness, scheduler)? - .is_none() + .is_some() { #[cfg(feature = "std")] println!("Added received Testcase"); @@ -312,12 +309,9 @@ where /// The llmp client needs to wait until a broker mapped all pages, before shutting down. /// Otherwise, the OS may already have removed the shared maps, fn await_restart_safe(&mut self) { - match &self.llmp { - llmp::LlmpConnection::IsClient { client } => { - // wait until we can drop the message safely. - client.await_save_to_unmap_blocking(); - } - _ => (), + if let llmp::LlmpConnection::IsClient { client } = &self.llmp { + // wait until we can drop the message safely. + client.await_save_to_unmap_blocking(); } } diff --git a/libafl/src/events/simple.rs b/libafl/src/events/simple.rs index ec91e2c1b1..2420a68eff 100644 --- a/libafl/src/events/simple.rs +++ b/libafl/src/events/simple.rs @@ -43,7 +43,7 @@ where OT: ObserversTuple, { let count = self.events.len(); - while self.events.len() > 0 { + while !self.events.is_empty() { let event = self.events.pop().unwrap(); self.handle_in_client(state, event)?; } @@ -118,11 +118,9 @@ where // Handle arriving events in the client fn handle_in_client(&mut self, _state: &mut S, event: Event) -> Result<(), Error> { - match event { - _ => Err(Error::Unknown(format!( - "Received illegal message that message should not have arrived: {:?}.", - event - ))), - } + Err(Error::Unknown(format!( + "Received illegal message that message should not have arrived: {:?}.", + event + ))) } } diff --git a/libafl/src/executors/inprocess.rs b/libafl/src/executors/inprocess.rs index 5c59f9df29..9cd91a940a 100644 --- a/libafl/src/executors/inprocess.rs +++ b/libafl/src/executors/inprocess.rs @@ -77,9 +77,7 @@ where { #[cfg(unix)] #[cfg(feature = "std")] - unsafe { - reset_oncrash_ptrs(); - } + reset_oncrash_ptrs(); Ok(()) } @@ -206,7 +204,7 @@ pub mod unix_signals { S: HasObjectives + HasSolutions, I: Input, { - if CURRENT_INPUT_PTR == ptr::null() { + if CURRENT_INPUT_PTR.is_null() { #[cfg(target_os = "android")] let si_addr = { ((info._pad[0] as usize) | ((info._pad[1] as usize) << 32)) as usize }; #[cfg(not(target_os = "android"))] @@ -250,19 +248,19 @@ pub mod unix_signals { let obj_fitness = state .objectives_mut() .is_interesting_all(&input, observers, ExitKind::Crash) - .expect("In crash handler objectives failure.".into()); + .expect("In crash handler objectives failure."); if obj_fitness > 0 { state .solutions_mut() .add(Testcase::new(input.clone())) - .expect("In crash handler solutions failure.".into()); + .expect("In crash handler solutions failure."); mgr.fire( state, Event::Objective { objective_size: state.solutions().count(), }, ) - .expect("Could not send crashing input".into()); + .expect("Could not send crashing input"); } mgr.on_restart(state).unwrap(); @@ -305,19 +303,19 @@ pub mod unix_signals { let obj_fitness = state .objectives_mut() .is_interesting_all(&input, observers, ExitKind::Crash) - .expect("In timeout handler objectives failure.".into()); + .expect("In timeout handler objectives failure."); if obj_fitness > 0 { state .solutions_mut() .add(Testcase::new(input.clone())) - .expect("In timeout handler solutions failure.".into()); + .expect("In timeout handler solutions failure."); mgr.fire( state, Event::Objective { objective_size: state.solutions().count(), }, ) - .expect("Could not send timeouting input".into()); + .expect("Could not send timeouting input"); } mgr.on_restart(state).unwrap(); @@ -331,6 +329,10 @@ pub mod unix_signals { std::process::exit(1); } + /// Sets the oncrash pointers before executing a single testcase + /// # Safety + /// As long as no signals are called, this is fine. + /// Once a signal occurs, the pointer needs to be up to date. #[inline] pub unsafe fn set_oncrash_ptrs( state: &mut S, @@ -344,14 +346,20 @@ pub mod unix_signals { OBSERVERS_PTR = observers as *const _ as *const c_void; } + /// Resets the oncrash pointers to null #[inline] - pub unsafe fn reset_oncrash_ptrs() { - CURRENT_INPUT_PTR = ptr::null(); - STATE_PTR = ptr::null_mut(); - EVENT_MGR_PTR = ptr::null_mut(); - OBSERVERS_PTR = ptr::null(); + pub fn reset_oncrash_ptrs() { + unsafe { + CURRENT_INPUT_PTR = ptr::null(); + STATE_PTR = ptr::null_mut(); + EVENT_MGR_PTR = ptr::null_mut(); + OBSERVERS_PTR = ptr::null(); + } } + /// Sets up the crash handler + /// # Safety + /// Everything using signals is unsafe. Don't do it unless you really need to. pub unsafe fn setup_crash_handlers() where EM: EventManager, diff --git a/libafl/src/executors/mod.rs b/libafl/src/executors/mod.rs index dfe2740203..82f01de97c 100644 --- a/libafl/src/executors/mod.rs +++ b/libafl/src/executors/mod.rs @@ -59,7 +59,7 @@ where I: Input + HasTargetBytes, { fn run_target(&mut self, input: &I) -> Result { - if input.target_bytes().as_slice().len() == 0 { + if input.target_bytes().as_slice().is_empty() { Err(Error::Empty("Input Empty".into())) } else { Ok(ExitKind::Ok) diff --git a/libafl/src/feedbacks/map.rs b/libafl/src/feedbacks/map.rs index 18daa5cd5a..fa4628a0e7 100644 --- a/libafl/src/feedbacks/map.rs +++ b/libafl/src/feedbacks/map.rs @@ -218,19 +218,13 @@ where } fn append_metadata(&mut self, testcase: &mut Testcase) -> Result<(), Error> { - match self.indexes.as_mut() { - Some(v) => { - let meta = MapIndexesMetadata::new(core::mem::take(v)); - testcase.add_metadata(meta); - } - None => {} + if let Some(v) = self.indexes.as_mut() { + let meta = MapIndexesMetadata::new(core::mem::take(v)); + testcase.add_metadata(meta); }; - match self.novelties.as_mut() { - Some(v) => { - let meta = MapNoveltiesMetadata::new(core::mem::take(v)); - testcase.add_metadata(meta); - } - None => {} + if let Some(v) = self.novelties.as_mut() { + let meta = MapNoveltiesMetadata::new(core::mem::take(v)); + testcase.add_metadata(meta); }; Ok(()) } diff --git a/libafl/src/feedbacks/mod.rs b/libafl/src/feedbacks/mod.rs index 76ac3abad5..617dadf1ae 100644 --- a/libafl/src/feedbacks/mod.rs +++ b/libafl/src/feedbacks/mod.rs @@ -184,3 +184,9 @@ impl CrashFeedback { Self {} } } + +impl Default for CrashFeedback { + fn default() -> Self { + Self::new() + } +} diff --git a/libafl/src/generators/mod.rs b/libafl/src/generators/mod.rs index 07ab0bd4e6..262381df96 100644 --- a/libafl/src/generators/mod.rs +++ b/libafl/src/generators/mod.rs @@ -93,7 +93,7 @@ where /// Generates up to DUMMY_BYTES_MAX non-random dummy bytes (0) fn generate_dummy(&self) -> BytesInput { let size = min(self.max_size, DUMMY_BYTES_MAX); - BytesInput::new(vec!['0' as u8; size]) + BytesInput::new(vec![0u8; size]) } } diff --git a/libafl/src/inputs/mod.rs b/libafl/src/inputs/mod.rs index 6d00b34c4e..cb409ed408 100644 --- a/libafl/src/inputs/mod.rs +++ b/libafl/src/inputs/mod.rs @@ -97,6 +97,10 @@ pub trait HasBytesVec { /// Has a length field pub trait HasLen { - /// The lenght + /// The length fn len(&self) -> usize; + + fn is_empty(&self) -> bool { + self.len() == 0 + } } diff --git a/libafl/src/mutators/mutations.rs b/libafl/src/mutators/mutations.rs index a4ac24c46d..0c405c8a51 100644 --- a/libafl/src/mutators/mutations.rs +++ b/libafl/src/mutators/mutations.rs @@ -41,7 +41,7 @@ where /// Mem move in the own vec #[inline] pub fn buffer_self_copy(data: &mut [u8], from: usize, to: usize, len: usize) { - debug_assert!(data.len() > 0); + debug_assert!(!data.is_empty()); debug_assert!(from + len <= data.len()); debug_assert!(to + len <= data.len()); if len != 0 && from != to { @@ -53,8 +53,8 @@ pub fn buffer_self_copy(data: &mut [u8], from: usize, to: usize, len: usize) { /// Mem move between vecs #[inline] pub fn buffer_copy(dst: &mut [u8], src: &[u8], from: usize, to: usize, len: usize) { - debug_assert!(dst.len() > 0); - debug_assert!(src.len() > 0); + debug_assert!(!dst.is_empty()); + debug_assert!(!src.is_empty()); debug_assert!(from + len <= src.len()); debug_assert!(to + len <= dst.len()); let dst_ptr = dst.as_mut_ptr(); @@ -124,7 +124,7 @@ where S: HasRand + HasMaxSize, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let bit = state.rand_mut().below((input.bytes().len() << 3) as u64) as usize; @@ -142,7 +142,7 @@ where S: HasRand, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let idx = state.rand_mut().below(input.bytes().len() as u64) as usize; @@ -160,7 +160,7 @@ where S: HasRand, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let idx = state.rand_mut().below(input.bytes().len() as u64) as usize; @@ -179,7 +179,7 @@ where S: HasRand, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let idx = state.rand_mut().below(input.bytes().len() as u64) as usize; @@ -198,7 +198,7 @@ where S: HasRand, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let idx = state.rand_mut().below(input.bytes().len() as u64) as usize; @@ -216,7 +216,7 @@ where S: HasRand, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let idx = state.rand_mut().below(input.bytes().len() as u64) as usize; @@ -234,7 +234,7 @@ where S: HasRand, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let idx = state.rand_mut().below(input.bytes().len() as u64) as usize; @@ -335,7 +335,7 @@ where S: HasRand, R: Rand, { - if input.bytes().len() == 0 { + if input.bytes().is_empty() { Ok(MutationResult::Skipped) } else { let idx = state.rand_mut().below(input.bytes().len() as u64) as usize; diff --git a/libafl/src/mutators/token_mutations.rs b/libafl/src/mutators/token_mutations.rs index 9a43602bfb..27ce1715a7 100644 --- a/libafl/src/mutators/token_mutations.rs +++ b/libafl/src/mutators/token_mutations.rs @@ -76,10 +76,10 @@ impl Tokens { // we are only interested in '"..."', not prefixed 'foo = ' let start = line.chars().nth(0); - if line.len() == 0 || start == Some('#') { + if line.is_empty() || start == Some('#') { continue; } - let pos_quote = match line.find("\"") { + let pos_quote = match line.find('\"') { Some(x) => x, _ => return Err(Error::IllegalArgument("Illegal line: ".to_owned() + line)), }; @@ -92,7 +92,7 @@ impl Tokens { Some(x) => x, _ => return Err(Error::IllegalArgument("Illegal line: ".to_owned() + line)), }; - if item.len() == 0 { + if item.is_empty() { continue; } @@ -134,7 +134,7 @@ where if meta.is_none() { return Ok(MutationResult::Skipped); } - if meta.unwrap().tokens().len() == 0 { + if meta.unwrap().tokens().is_empty() { return Ok(MutationResult::Skipped); } meta.unwrap().tokens().len() @@ -180,7 +180,7 @@ where if meta.is_none() { return Ok(MutationResult::Skipped); } - if meta.unwrap().tokens().len() == 0 { + if meta.unwrap().tokens().is_empty() { return Ok(MutationResult::Skipped); } meta.unwrap().tokens().len() diff --git a/libafl/src/observers/map.rs b/libafl/src/observers/map.rs index a63155c030..8c6fa0ef00 100644 --- a/libafl/src/observers/map.rs +++ b/libafl/src/observers/map.rs @@ -118,7 +118,7 @@ where { /// Creates a new MapObserver pub fn new(name: &'static str, map: &'static mut [T]) -> Self { - let initial = if map.len() > 0 { map[0] } else { T::default() }; + let initial = if map.is_empty() { T::default() } else { map[0] }; Self { map: ArrayMut::Cptr((map.as_mut_ptr(), map.len())), name: name.to_string(), @@ -128,13 +128,15 @@ where /// Creates a new MapObserver from a raw pointer pub fn new_from_ptr(name: &'static str, map_ptr: *mut T, len: usize) -> Self { - unsafe { - let initial = if len > 0 { *map_ptr } else { T::default() }; - StdMapObserver { - map: ArrayMut::Cptr((map_ptr, len)), - name: name.to_string(), - initial, - } + let initial = if len > 0 { + unsafe { *map_ptr } + } else { + T::default() + }; + StdMapObserver { + map: ArrayMut::Cptr((map_ptr, len)), + name: name.to_string(), + initial, } } } @@ -229,14 +231,16 @@ where max_len: usize, size_ptr: *const usize, ) -> Self { - unsafe { - let initial = if max_len > 0 { *map_ptr } else { T::default() }; - VariableMapObserver { - map: ArrayMut::Cptr((map_ptr, max_len)), - size: Cptr::Cptr(size_ptr), - name: name.into(), - initial, - } + let initial = if max_len > 0 { + unsafe { *map_ptr } + } else { + T::default() + }; + VariableMapObserver { + map: ArrayMut::Cptr((map_ptr, max_len)), + size: Cptr::Cptr(size_ptr), + name: name.into(), + initial, } } } diff --git a/libafl/src/state/mod.rs b/libafl/src/state/mod.rs index ef6a467eae..ae771d703d 100644 --- a/libafl/src/state/mod.rs +++ b/libafl/src/state/mod.rs @@ -515,9 +515,9 @@ where self.solutions_mut().add(Testcase::new(input.clone()))?; } - if !self + if self .add_if_interesting(&input, fitness, scheduler)? - .is_none() + .is_some() { let observers_buf = manager.serialize_observers(observers)?; manager.fire( @@ -564,7 +564,7 @@ where let path = entry.path(); let attributes = fs::metadata(&path); - if !attributes.is_ok() { + if attributes.is_err() { continue; }