From d6cbe5d014c8c02ed4077ee702d34ea442c62e47 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 14 Jul 2022 14:27:24 -0400 Subject: [PATCH] ensure that RangeMap panics on OOB --- src/range_map.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/range_map.rs b/src/range_map.rs index 474ad9dcccd..c77ea63b087 100644 --- a/src/range_map.rs +++ b/src/range_map.rs @@ -77,6 +77,10 @@ pub fn iter(&self, offset: Size, len: Size) -> impl Iterator }; // The first offset that is not included any more. let end = offset + len; + assert!( + end <= self.v.last().unwrap().range.end, + "iterating beyond the bounds of this RangeMap" + ); slice .iter() .take_while(move |elem| elem.range.start < end) @@ -279,4 +283,18 @@ fn gaps() { assert_eq!(map.v.len(), 5); 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::::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::::new(Size::from_bytes(20), -1); + let _ = map.iter(Size::from_bytes(11), Size::from_bytes(11)); + } }