Skip to content

Commit 35fc054

Browse files
KubuxuStebalien
authored andcommitted
Fix bf.unset(u64::MAX) causing delayed panic on bf.ranges()
Detected by rle_encode fuzz target. Caused by overflow when trying to compute input ranges into difference operation within `ranges()`. Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 9007681 commit 35fc054

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

ipld/bitfield/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ impl BitField {
170170

171171
/// Removes the bit at a given index from the bit field.
172172
pub fn unset(&mut self, bit: u64) {
173+
if bit == u64::MAX {
174+
return;
175+
}
173176
self.set.remove(&bit);
174177
self.unset.insert(bit);
175178
}

ipld/bitfield/src/rleplus/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,20 @@ mod tests {
509509
assert_eq!(2, last);
510510
}
511511

512+
#[test]
513+
fn test_unset_max() {
514+
// Create any bitfield
515+
let ranges: Vec<u64> = vec![0, 1, 2, 3];
516+
let iter = ranges_from_bits(ranges);
517+
let mut bf = BitField::from_ranges(iter);
518+
519+
// Unset u64::MAX
520+
bf.unset(u64::MAX);
521+
522+
let last = bf.ranges().last().unwrap();
523+
assert_eq!(0..4, last);
524+
}
525+
512526
#[test]
513527
fn test_zero_last() {
514528
let mut bf = BitField::new();

0 commit comments

Comments
 (0)