Skip to content

Commit

Permalink
Fix #2266 where the type of functions when used as RValues was not pr…
Browse files Browse the repository at this point in the history
…operly printed.
  • Loading branch information
DarkaMaul committed Apr 8, 2024
1 parent 118c916 commit 114dc2e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 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;
}
}
16 changes: 16 additions & 0 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,3 +35,18 @@ def test_inheritance_printer(solc_binary_path) -> None:

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


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 114dc2e

Please sign in to comment.