Skip to content

Commit

Permalink
fix(fuzz): prevent int strategy to overflow when complicate
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Mar 19, 2024
1 parent 0026488 commit 78d976e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
28 changes: 27 additions & 1 deletion crates/evm/fuzz/src/strategies/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ impl ValueTree for IntValueTree {
return false
}

self.lo = self.curr + if self.hi.is_negative() { I256::MINUS_ONE } else { I256::ONE };
self.lo = if self.curr != I256::MIN && self.curr != I256::MAX {
self.curr + if self.hi.is_negative() { I256::MINUS_ONE } else { I256::ONE }
} else {
self.curr
};

self.reposition()
}
Expand Down Expand Up @@ -192,3 +196,25 @@ impl Strategy for IntStrategy {
}
}
}

#[cfg(test)]
mod tests {
use crate::strategies::int::IntValueTree;
use alloy_primitives::I256;
use proptest::strategy::ValueTree;

#[test]
fn test_int_tree_complicate_should_not_overflow() {
let mut int_tree = IntValueTree::new(I256::MAX, false);
assert_eq!(int_tree.hi, I256::MAX);
assert_eq!(int_tree.curr, I256::MAX);
int_tree.complicate();
assert_eq!(int_tree.lo, I256::MAX);

let mut int_tree = IntValueTree::new(I256::MIN, false);
assert_eq!(int_tree.hi, I256::MIN);
assert_eq!(int_tree.curr, I256::MIN);
int_tree.complicate();
assert_eq!(int_tree.lo, I256::MIN);
}
}
16 changes: 16 additions & 0 deletions crates/evm/fuzz/src/strategies/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,19 @@ impl Strategy for UintStrategy {
}
}
}

#[cfg(test)]
mod tests {
use crate::strategies::uint::UintValueTree;
use alloy_primitives::U256;
use proptest::strategy::ValueTree;

#[test]
fn test_uint_tree_complicate_max() {
let mut uint_tree = UintValueTree::new(U256::MAX, false);
assert_eq!(uint_tree.hi, U256::MAX);
assert_eq!(uint_tree.curr, U256::MAX);
uint_tree.complicate();
assert_eq!(uint_tree.lo, U256::MIN);
}
}

0 comments on commit 78d976e

Please sign in to comment.