Skip to content

Commit

Permalink
fix: use contract declarer's scope for name resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalpharush committed May 17, 2024
1 parent 08d631f commit 22b9cfc
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
2 changes: 1 addition & 1 deletion slither/solc_parsing/expressions/find_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def _find_variable_init(
scope = underlying_function.file_scope
else:
assert isinstance(underlying_function, FunctionContract)
scope = underlying_function.contract.file_scope
scope = underlying_function.contract_declarer.file_scope

elif isinstance(caller_context, StructureTopLevelSolc):
direct_contracts = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {
ParentContract
} from "./ParentContract.sol";

import {
MainErrors as Errors
} from "./../errors/MainErrors.sol";


contract MainContract is ParentContract {


function functionWithMainError1(uint256 a, uint256 b) external pure returns (uint256) {
if (a == b) {
revert Errors.MainError1();
}
// Add some arithmetic operations here
return a + b;
}

function functionWithMainError2(uint256 a, uint256 b) external pure returns (uint256) {
if (a < b) {
revert Errors.MainError2();
}
// Add some arithmetic operations here
return a - b;
}

function functionWithMainError3(uint256 a, uint256 b) external pure returns (uint256) {
if (b == 0) {
revert Errors.MainError3();
}
// Add some arithmetic operations here
return a * b;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;


import {
AccessControlErrors as Errors
} from "../errors/ParentContractErrors.sol";


contract ParentContract {


function functionWithAccessControlErrors1(uint256 a, uint256 b) external pure returns (uint256) {
if (a == b) {
revert Errors.AccessControlErrors1();
}
// Add some arithmetic operations here
return a + b;
}

function functionWithAccessControlErrors2(uint256 a, uint256 b) external pure returns (uint256) {
if (a < b) {
revert Errors.AccessControlErrors2();
}
// Add some arithmetic operations here
return a - b;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

// TODO: remove unused errors
library MainErrors {
error MainError1();
error MainError2();
error MainError3();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

library AccessControlErrors {
error AccessControlErrors1();
error AccessControlErrors2();
}
17 changes: 17 additions & 0 deletions tests/unit/core/test_scope_with_renaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pathlib import Path
from crytic_compile import CryticCompile
from crytic_compile.platform.solc_standard_json import SolcStandardJson

from slither import Slither

TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data"
SCOPE_RENAMING_TEST_DATA_DIR = Path(TEST_DATA_DIR, "scope_with_renaming")

# https://github.com/crytic/slither/issues/2454
def test_find_variable_scope_with_renaming(solc_binary_path) -> None:
solc_path = solc_binary_path("0.8.24")
standard_json = SolcStandardJson()
for source_file in SCOPE_RENAMING_TEST_DATA_DIR.rglob("**/*.sol"):
standard_json.add_source_file(Path(source_file).as_posix())
compilation = CryticCompile(standard_json, solc=solc_path)
Slither(compilation, disallow_partial=True)
2 changes: 1 addition & 1 deletion tests/unit/core/test_using_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_using_for_global_collision(solc_binary_path) -> None:
for source_file in Path(USING_FOR_TEST_DATA_DIR, "using_for_global_collision").rglob("*.sol"):
standard_json.add_source_file(Path(source_file).as_posix())
compilation = CryticCompile(standard_json, solc=solc_path)
sl = Slither(compilation)
sl = Slither(compilation, disallow_partial=True)
_run_all_detectors(sl)


Expand Down

0 comments on commit 22b9cfc

Please sign in to comment.