Skip to content

Commit 4d7012c

Browse files
committed
Fix BlockRangeSequence with a start greater than its end
It will yied an empty iterator, else it would crash with an overflow when calling `is_empty` since it subtract start to end.
1 parent 045d549 commit 4d7012c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

mithril-common/src/entities/block_range.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ impl BlockRangesSequence {
168168
// End is inclusive, so we need to add 1
169169
let end = BlockRange::start(*interval.end() + 1);
170170

171-
Self { start, end }
171+
if start >= end {
172+
Self { start: 0, end: 0 }
173+
} else {
174+
Self { start, end }
175+
}
172176
}
173177

174178
/// Returns the start of the block ranges sequence.
@@ -188,7 +192,7 @@ impl BlockRangesSequence {
188192

189193
/// Returns `true` if the block ranges sequence contains no elements.
190194
pub fn is_empty(&self) -> bool {
191-
self.len() == 0
195+
self.start >= self.end
192196
}
193197

194198
/// Consume `self` into a new Vec
@@ -201,7 +205,7 @@ impl Iterator for BlockRangesSequence {
201205
type Item = BlockRange;
202206

203207
fn next(&mut self) -> Option<Self::Item> {
204-
if self.start >= self.end {
208+
if BlockRangesSequence::is_empty(self) {
205209
return None;
206210
}
207211

@@ -402,4 +406,13 @@ mod tests {
402406
BlockRange::from_block_number_and_length(10, 0)
403407
.expect_err("BlockRange should not be computed with a length of 0");
404408
}
409+
410+
#[test]
411+
// allow to specify a range with start > end
412+
#[allow(clippy::reversed_empty_ranges)]
413+
fn test_building_sequence_with_start_greater_than_end_yield_empty_iterator() {
414+
let sequence = BlockRange::all_block_ranges_in(30..=15);
415+
assert_eq!(sequence.clone().into_vec(), vec![]);
416+
assert!(sequence.is_empty());
417+
}
405418
}

0 commit comments

Comments
 (0)