Skip to content

Commit

Permalink
Merge pull request #2412 from DarkaMaul/fix/ir-assignment-2266
Browse files Browse the repository at this point in the history
Fix #2266
  • Loading branch information
montyly authored Oct 2, 2024
2 parents 1ed8205 + eaebe7a commit 546122a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
13 changes: 11 additions & 2 deletions slither/slithir/operations/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ def rvalue(self) -> Union[RVALUE, Function, TupleVariable]:

def __str__(self) -> str:
lvalue = self.lvalue

# When rvalues are functions, we want to properly display their return type
# Fix: https://github.com/crytic/slither/issues/2266
if isinstance(self.rvalue.type, list):
rvalue_type = ",".join(f"{rvalue_type}" for rvalue_type in self.rvalue.type)
else:
rvalue_type = f"{self.rvalue.type}"

assert lvalue
if lvalue and isinstance(lvalue, ReferenceVariable):
points = lvalue.points_to
while isinstance(points, ReferenceVariable):
points = points.points_to
return f"{lvalue}({lvalue.type}) (->{points}) := {self.rvalue}({self.rvalue.type})"
return f"{lvalue}({lvalue.type}) := {self.rvalue}({self.rvalue.type})"
return f"{lvalue}({lvalue.type}) (->{points}) := {self.rvalue}({rvalue_type})"

return f"{lvalue}({lvalue.type}) := {self.rvalue}({rvalue_type})"
13 changes: 13 additions & 0 deletions tests/e2e/printers/test_data/test_printer_slithir/bug-2266.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pragma solidity ^0.8.0;

contract A {
function add(uint256 a, uint256 b) public returns (uint256) {
return a + b;
}
}

contract B is A {
function assignFunction() public {
function(uint256, uint256) returns (uint256) myFunction = super.add;
}
}
19 changes: 17 additions & 2 deletions tests/e2e/printers/test_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from slither import Slither
from slither.printers.inheritance.inheritance_graph import PrinterInheritanceGraph
from slither.printers.summary.slithir import PrinterSlithIR


TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data"
Expand Down Expand Up @@ -34,8 +35,7 @@ def test_inheritance_printer(solc_binary_path) -> None:

assert counter["B -> A"] == 2
assert counter["C -> A"] == 1

# Lets also test the include/exclude interface behavior
# Let also test the include/exclude interface behavior
# Check that the interface is not included
assert "MyInterfaceX" not in content

Expand All @@ -46,3 +46,18 @@ def test_inheritance_printer(solc_binary_path) -> None:

# Remove test generated files
Path("test_printer.dot").unlink(missing_ok=True)


def test_slithir_printer(solc_binary_path) -> None:
solc_path = solc_binary_path("0.8.0")
standard_json = SolcStandardJson()
standard_json.add_source_file(
Path(TEST_DATA_DIR, "test_printer_slithir", "bug-2266.sol").as_posix()
)
compilation = CryticCompile(standard_json, solc=solc_path)
slither = Slither(compilation)

printer = PrinterSlithIR(slither, logger=None)
output = printer.output("test_printer_slithir.dot")

assert "slither.core.solidity_types" not in output.data["description"]

0 comments on commit 546122a

Please sign in to comment.