Skip to content

Commit d847e0f

Browse files
authored
fix(forge): avoid panic when empty fuzz selectors in invariants (#9076)
1 parent a96b826 commit d847e0f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

crates/evm/evm/src/executors/invariant/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,6 @@ impl<'a> InvariantExecutor<'a> {
742742
) -> Result<()> {
743743
for (address, (identifier, _)) in self.setup_contracts.iter() {
744744
if let Some(selectors) = self.artifact_filters.targeted.get(identifier) {
745-
if selectors.is_empty() {
746-
continue;
747-
}
748745
self.add_address_with_functions(*address, selectors, false, targeted_contracts)?;
749746
}
750747
}
@@ -774,6 +771,11 @@ impl<'a> InvariantExecutor<'a> {
774771
should_exclude: bool,
775772
targeted_contracts: &mut TargetedContracts,
776773
) -> eyre::Result<()> {
774+
// Do not add address in target contracts if no function selected.
775+
if selectors.is_empty() {
776+
return Ok(())
777+
}
778+
777779
let contract = match targeted_contracts.entry(address) {
778780
Entry::Occupied(entry) => entry.into_mut(),
779781
Entry::Vacant(entry) => {

crates/forge/tests/it/invariant.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,35 @@ contract BalanceAssumeTest is Test {
823823
...
824824
"#]]);
825825
});
826+
827+
// Test proper message displayed if `targetSelector`/`excludeSelector` called with empty selectors.
828+
// <https://github.com/foundry-rs/foundry/issues/9066>
829+
forgetest_init!(should_not_panic_if_no_selectors, |prj, cmd| {
830+
prj.add_test(
831+
"NoSelectorTest.t.sol",
832+
r#"
833+
import {Test} from "forge-std/Test.sol";
834+
835+
contract TestHandler is Test {}
836+
837+
contract NoSelectorTest is Test {
838+
bytes4[] selectors;
839+
840+
function setUp() public {
841+
TestHandler handler = new TestHandler();
842+
targetSelector(FuzzSelector({addr: address(handler), selectors: selectors}));
843+
excludeSelector(FuzzSelector({addr: address(handler), selectors: selectors}));
844+
}
845+
846+
function invariant_panic() public {}
847+
}
848+
"#,
849+
)
850+
.unwrap();
851+
852+
cmd.args(["test", "--mt", "invariant_panic"]).assert_failure().stdout_eq(str![[r#"
853+
...
854+
[FAIL: failed to set up invariant testing environment: No contracts to fuzz.] invariant_panic() (runs: 0, calls: 0, reverts: 0)
855+
...
856+
"#]]);
857+
});

0 commit comments

Comments
 (0)