From c561182f072c581fb22078dcf879f225f3020cfd Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Thu, 3 Feb 2022 14:25:25 +0100 Subject: [PATCH] Set map observers initial value to T::default() on creation (#520) --- libafl/src/observers/map.rs | 41 +++++++++---------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/libafl/src/observers/map.rs b/libafl/src/observers/map.rs index f9eee07fa7..9d2d1d472c 100644 --- a/libafl/src/observers/map.rs +++ b/libafl/src/observers/map.rs @@ -255,22 +255,20 @@ where /// Creates a new [`MapObserver`] #[must_use] pub fn new(name: &'static str, map: &'a mut [T]) -> Self { - let initial = if map.is_empty() { T::default() } else { map[0] }; Self { map: OwnedSliceMut::from(map), name: name.to_string(), - initial, + initial: T::default(), } } /// Creates a new [`MapObserver`] with an owned map #[must_use] pub fn new_owned(name: &'static str, map: Vec) -> Self { - let initial = if map.is_empty() { T::default() } else { map[0] }; Self { map: OwnedSliceMut::from(map), name: name.to_string(), - initial, + initial: T::default(), } } @@ -280,16 +278,10 @@ where /// Will dereference the owned slice with up to len elements. #[must_use] pub fn new_from_ownedref(name: &'static str, map: OwnedSliceMut<'a, T>) -> Self { - let map_slice = map.as_slice(); - let initial = if map_slice.is_empty() { - T::default() - } else { - map_slice[0] - }; Self { map, name: name.to_string(), - initial, + initial: T::default(), } } @@ -298,11 +290,10 @@ where /// # Safety /// Will dereference the `map_ptr` with up to len elements. pub unsafe fn new_from_ptr(name: &'static str, map_ptr: *mut T, len: usize) -> Self { - let initial = if len > 0 { *map_ptr } else { T::default() }; StdMapObserver { map: OwnedSliceMut::from_raw_parts_mut(map_ptr, len), name: name.to_string(), - initial, + initial: T::default(), } } } @@ -447,11 +438,10 @@ where #[must_use] pub fn new(name: &'static str, map: &'a mut [T]) -> Self { assert!(map.len() >= N); - let initial = if map.is_empty() { T::default() } else { map[0] }; Self { map: OwnedSliceMut::from(map), name: name.to_string(), - initial, + initial: T::default(), } } @@ -472,11 +462,10 @@ where /// # Safety /// Will dereference the `map_ptr` with up to len elements. pub unsafe fn new_from_ptr(name: &'static str, map_ptr: *mut T) -> Self { - let initial = if N > 0 { *map_ptr } else { T::default() }; ConstMapObserver { map: OwnedSliceMut::from_raw_parts_mut(map_ptr, N), name: name.to_string(), - initial, + initial: T::default(), } } } @@ -617,12 +606,11 @@ where { /// Creates a new [`MapObserver`] pub fn new(name: &'static str, map: &'a mut [T], size: &'a mut usize) -> Self { - let initial = if map.is_empty() { T::default() } else { map[0] }; Self { map: OwnedSliceMut::from(map), size: OwnedRefMut::Ref(size), name: name.into(), - initial, + initial: T::default(), } } @@ -636,12 +624,11 @@ where max_len: usize, size: &'a mut usize, ) -> Self { - let initial = if max_len > 0 { *map_ptr } else { T::default() }; VariableMapObserver { map: OwnedSliceMut::from_raw_parts_mut(map_ptr, max_len), size: OwnedRefMut::Ref(size), name: name.into(), - initial, + initial: T::default(), } } } @@ -917,14 +904,10 @@ where pub fn new(name: &'static str, maps: &'a mut [&'a mut [T]]) -> Self { let mut idx = 0; let mut v = 0; - let mut initial = T::default(); let mut builder = vec![]; let maps: Vec<_> = maps .iter_mut() .map(|x| { - if !x.is_empty() { - initial = x[0]; - } let l = x.len(); let r = (idx..(idx + l), v); idx += l; @@ -938,7 +921,7 @@ where intervals: builder.into_iter().collect::>(), len: idx, name: name.to_string(), - initial, + initial: T::default(), iter_idx: 0, } } @@ -948,14 +931,10 @@ where pub fn new_owned(name: &'static str, maps: Vec>) -> Self { let mut idx = 0; let mut v = 0; - let mut initial = T::default(); let mut builder = vec![]; let maps: Vec<_> = maps .into_iter() .map(|x| { - if !x.is_empty() { - initial = x[0]; - } let l = x.len(); let r = (idx..(idx + l), v); idx += l; @@ -969,7 +948,7 @@ where intervals: builder.into_iter().collect::>(), len: idx, name: name.to_string(), - initial, + initial: T::default(), iter_idx: 0, } }