Skip to content

Commit

Permalink
fix(forge): avoid panic when empty fuzz selectors in invariants (#9…
Browse files Browse the repository at this point in the history
…076)
  • Loading branch information
grandizzy authored Oct 9, 2024
1 parent a96b826 commit d847e0f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
8 changes: 5 additions & 3 deletions crates/evm/evm/src/executors/invariant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,6 @@ impl<'a> InvariantExecutor<'a> {
) -> Result<()> {
for (address, (identifier, _)) in self.setup_contracts.iter() {
if let Some(selectors) = self.artifact_filters.targeted.get(identifier) {
if selectors.is_empty() {
continue;
}
self.add_address_with_functions(*address, selectors, false, targeted_contracts)?;
}
}
Expand Down Expand Up @@ -774,6 +771,11 @@ impl<'a> InvariantExecutor<'a> {
should_exclude: bool,
targeted_contracts: &mut TargetedContracts,
) -> eyre::Result<()> {
// Do not add address in target contracts if no function selected.
if selectors.is_empty() {
return Ok(())
}

let contract = match targeted_contracts.entry(address) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
Expand Down
32 changes: 32 additions & 0 deletions crates/forge/tests/it/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,35 @@ contract BalanceAssumeTest is Test {
...
"#]]);
});

// Test proper message displayed if `targetSelector`/`excludeSelector` called with empty selectors.
// <https://github.com/foundry-rs/foundry/issues/9066>
forgetest_init!(should_not_panic_if_no_selectors, |prj, cmd| {
prj.add_test(
"NoSelectorTest.t.sol",
r#"
import {Test} from "forge-std/Test.sol";
contract TestHandler is Test {}
contract NoSelectorTest is Test {
bytes4[] selectors;
function setUp() public {
TestHandler handler = new TestHandler();
targetSelector(FuzzSelector({addr: address(handler), selectors: selectors}));
excludeSelector(FuzzSelector({addr: address(handler), selectors: selectors}));
}
function invariant_panic() public {}
}
"#,
)
.unwrap();

cmd.args(["test", "--mt", "invariant_panic"]).assert_failure().stdout_eq(str![[r#"
...
[FAIL: failed to set up invariant testing environment: No contracts to fuzz.] invariant_panic() (runs: 0, calls: 0, reverts: 0)
...
"#]]);
});

0 comments on commit d847e0f

Please sign in to comment.