ensure that RangeMap panics on OOB

This commit is contained in:
Ralf Jung 2022-07-14 14:27:24 -04:00
parent 421f92bee6
commit d6cbe5d014

View File

@ -77,6 +77,10 @@ pub fn iter(&self, offset: Size, len: Size) -> impl Iterator<Item = (Size, &T)>
}; };
// The first offset that is not included any more. // The first offset that is not included any more.
let end = offset + len; let end = offset + len;
assert!(
end <= self.v.last().unwrap().range.end,
"iterating beyond the bounds of this RangeMap"
);
slice slice
.iter() .iter()
.take_while(move |elem| elem.range.start < end) .take_while(move |elem| elem.range.start < end)
@ -279,4 +283,18 @@ fn gaps() {
assert_eq!(map.v.len(), 5); assert_eq!(map.v.len(), 5);
assert_eq!(to_vec(&map, 10, 10), vec![23, 42, 23, 23, 23, 19, 19, 19, 19, 19]); assert_eq!(to_vec(&map, 10, 10), vec![23, 42, 23, 23, 23, 19, 19, 19, 19, 19]);
} }
#[test]
#[should_panic]
fn out_of_range_iter_mut() {
let mut map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
let _ = map.iter_mut(Size::from_bytes(11), Size::from_bytes(11));
}
#[test]
#[should_panic]
fn out_of_range_iter() {
let map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
let _ = map.iter(Size::from_bytes(11), Size::from_bytes(11));
}
} }