Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ipld/bitfield/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,16 @@ impl BitField {

/// Returns the number of set bits in the bit field.
pub fn len(&self) -> u64 {
self.ranges().map(|range| range.size()).sum()
(self.set.len() as u64)
+ self
.ranges
.iter()
.map(|range| {
range.size()
- (self.unset.range(range.clone()).count() as u64)
- (self.set.range(range.clone()).count() as u64)
Comment on lines +330 to +332
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the correctness of the implementation per se, probably some more (fuzz?) tests would do.

In this particular case, I see a potential danger of integer underflow, i.e., if range.size() < ((self.unset.range(range.clone()).count() as u64) + (self.set.range(range.clone()).count() as u64)) then some absurd values could get produced. Given we have overflow-checks = true for wasm profile, it'd probably panic (bad). Can we guarantee the condition above doesn't happen? Is it already guaranteed from some internal logic? If so, some comment to reassure the reader would help.

})
.sum::<u64>()
}

/// Returns a new bit field containing the bits in `self` that remain
Expand Down
Loading