Skip to content

Commit

Permalink
fix: handle possible underflow in smt
Browse files Browse the repository at this point in the history
A few unchecked subtractions are replaced with checked alternatives.
  • Loading branch information
CjS77 committed Sep 14, 2023
1 parent d630d11 commit 0121fef
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 0121fef

Please sign in to comment.