Skip to content

Commit

Permalink
Merge pull request #2405 from /pull/2331/head
Browse files Browse the repository at this point in the history
add test for #2331
  • Loading branch information
0xalpharush authored Apr 7, 2024
2 parents 7071098 + 4cf8c9a commit 8f018b1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion slither/core/solidity_types/array_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __str__(self) -> str:
def __eq__(self, other: Any) -> bool:
if not isinstance(other, ArrayType):
return False
return self._type == other.type and self.length == other.length
return self._type == other.type and self._length_value == other.length_value

def __hash__(self) -> int:
return hash(str(self))
70 changes: 52 additions & 18 deletions tests/unit/core/test_using_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ def test_using_for_top_level_same_name(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint256)")
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "a":
return
assert False
found = True
assert found


def test_using_for_top_level_implicit_conversion(solc_binary_path) -> None:
Expand All @@ -41,10 +42,11 @@ def test_using_for_top_level_implicit_conversion(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint16)")
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "f":
return
assert False
found = True
assert found


def test_using_for_alias_top_level(solc_binary_path) -> None:
Expand All @@ -55,17 +57,18 @@ def test_using_for_alias_top_level(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint256)")
ok = False
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "b":
ok = True
if not ok:
assert False
found = True
assert found

found = False
topLevelCall = contract_c.get_function_from_full_name("topLevel(uint256)")
for ir in topLevelCall.all_slithir_operations():
if isinstance(ir, InternalCall) and ir.function_name == "a":
return
assert False
found = True
assert found


def test_using_for_alias_contract(solc_binary_path) -> None:
Expand All @@ -76,17 +79,19 @@ def test_using_for_alias_contract(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint256)")
ok = False
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "b":
ok = True
if not ok:
assert False
found = True

assert found

found = False
topLevelCall = contract_c.get_function_from_full_name("topLevel(uint256)")
for ir in topLevelCall.all_slithir_operations():
if isinstance(ir, InternalCall) and ir.function_name == "a":
return
assert False
found = True
assert found


def test_using_for_in_library(solc_binary_path) -> None:
Expand All @@ -96,7 +101,36 @@ def test_using_for_in_library(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("A")[0]
libCall = contract_c.get_function_from_full_name("a(uint256)")
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "B" and ir.function_name == "b":
return
assert False
found = True
assert found


def test_using_for_constant_folding(slither_from_solidity_source) -> None:
# https://github.com/crytic/slither/issues/2307
source = """
library SafeMath {
uint256 private constant twelve = 12;
struct A {uint256 a;}
function add(A[twelve] storage z) internal { }
}
contract MathContract {
uint256 private constant twelve = 12;
using SafeMath for SafeMath.A[twelve];
SafeMath.A[twelve] public z;
function safeAdd() public {
z.add();
}
}
"""
with slither_from_solidity_source(source) as slither:
contract = slither.get_contract_from_name("MathContract")[0]
add = contract.get_function_from_full_name("safeAdd()")
found = False
for ir in add.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.function_name == "add":
found = True
assert found

0 comments on commit 8f018b1

Please sign in to comment.