Skip to content

Commit f76309d

Browse files
authored
fix(forge): avoid panic on internal decoding of linked tests (#10333)
Failing tests related to etherscan sepolia migration
1 parent 41506ca commit f76309d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

crates/evm/traces/src/debug/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ impl<'a> DebugStepsWalker<'a> {
200200
fn parse_function_from_loc(source: &SourceData, loc: &SourceElement) -> Option<String> {
201201
let start = loc.offset() as usize;
202202
let end = start + loc.length() as usize;
203+
let src_len = source.source.len();
204+
205+
// Handle special case of preprocessed test sources.
206+
if start > src_len || end > src_len {
207+
return None;
208+
}
209+
203210
let source_part = &source.source[start..end];
204211
if !source_part.starts_with("function") {
205212
return None;

crates/forge/tests/cli/test_optimizer.rs

+66
Original file line numberDiff line numberDiff line change
@@ -1345,3 +1345,69 @@ Compiling 20 files with [..]
13451345
13461346
"#]]);
13471347
});
1348+
1349+
// Test preprocessed contracts with decode internal fns.
1350+
forgetest_init!(preprocess_contract_with_decode_internal, |prj, cmd| {
1351+
prj.update_config(|config| {
1352+
config.dynamic_test_linking = true;
1353+
});
1354+
1355+
prj.add_test(
1356+
"Counter.t.sol",
1357+
r#"
1358+
import {Test} from "forge-std/Test.sol";
1359+
import {Counter} from "../src/Counter.sol";
1360+
1361+
contract CounterTest is Test {
1362+
Counter public counter;
1363+
1364+
function setUp() public {
1365+
create_counter(0);
1366+
}
1367+
1368+
function test_Increment() public {
1369+
create_counter(0);
1370+
counter.increment();
1371+
assertEq(counter.number(), 1);
1372+
}
1373+
1374+
function create_counter(uint256 number) internal {
1375+
counter = new Counter();
1376+
counter.setNumber(number);
1377+
}
1378+
}
1379+
"#,
1380+
)
1381+
.unwrap();
1382+
1383+
cmd.args(["test", "--decode-internal", "-vvvv"]).assert_success().stdout_eq(str![[r#"
1384+
[COMPILING_FILES] with [SOLC_VERSION]
1385+
[SOLC_VERSION] [ELAPSED]
1386+
Compiler run successful!
1387+
1388+
Ran 1 test for test/Counter.t.sol:CounterTest
1389+
[PASS] test_Increment() ([GAS])
1390+
Traces:
1391+
[..] CounterTest::test_Increment()
1392+
├─ [0] VM::deployCode("src/Counter.sol:Counter")
1393+
│ ├─ [96345] → new Counter@0x2e234DAe75C793f67A35089C9d99245E1C58470b
1394+
│ │ └─ ← [Return] 481 bytes of code
1395+
│ └─ ← [Return] Counter: [0x2e234DAe75C793f67A35089C9d99245E1C58470b]
1396+
├─ [..] Counter::setNumber(0)
1397+
│ └─ ← [Stop]
1398+
├─ [..] Counter::increment()
1399+
│ └─ ← [Stop]
1400+
├─ [..] Counter::number() [staticcall]
1401+
│ └─ ← [Return] 1
1402+
├─ [..] StdAssertions::assertEq(1, 1)
1403+
│ ├─ [0] VM::assertEq(1, 1) [staticcall]
1404+
│ │ └─ ← [Return]
1405+
│ └─ ←
1406+
└─ ← [Stop]
1407+
1408+
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
1409+
1410+
Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
1411+
1412+
"#]]);
1413+
});

0 commit comments

Comments
 (0)