Skip to content

Commit

Permalink
fix: handle possible underflow in smt (#5769)
Browse files Browse the repository at this point in the history
A few unchecked subtractions are replaced with checked alternatives.


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify
  • Loading branch information
CjS77 authored Sep 14, 2023
1 parent d630d11 commit 558e6f2
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions base_layer/mmr/src/sparse_merkle_tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,20 @@ impl<'a> DoubleEndedIterator for PathIterator<'a> {
if self.cursor_front >= self.cursor_back {
return None;
}
self.cursor_back -= 1;
self.cursor_back = self.cursor_back.checked_sub(1)?;
let bit = get_bit(self.key.as_slice(), self.cursor_back);
Some(bit_to_dir(bit))
}

fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.cursor_back -= n;
self.cursor_back = self.cursor_back.checked_sub(n)?;
self.next_back()
}
}

impl<'a> ExactSizeIterator for PathIterator<'a> {
fn len(&self) -> usize {
self.cursor_back - self.cursor_front
self.cursor_back.saturating_sub(self.cursor_front)
}
}

Expand Down

0 comments on commit 558e6f2

Please sign in to comment.