Fix #2266 where the type of functions when used as RValues was not properly printed.

pull/2412/head
Alexis 8 months ago
parent 118c916b35
commit 114dc2eeca
No known key found for this signature in database
  1. 13
      slither/slithir/operations/assignment.py
  2. 13
      tests/e2e/printers/test_data/test_printer_slithir/bug-2266.sol
  3. 16
      tests/e2e/printers/test_printers.py

@ -45,10 +45,19 @@ class Assignment(OperationWithLValue):
def __str__(self) -> str: def __str__(self) -> str:
lvalue = self.lvalue 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 assert lvalue
if lvalue and isinstance(lvalue, ReferenceVariable): if lvalue and isinstance(lvalue, ReferenceVariable):
points = lvalue.points_to points = lvalue.points_to
while isinstance(points, ReferenceVariable): while isinstance(points, ReferenceVariable):
points = points.points_to points = points.points_to
return f"{lvalue}({lvalue.type}) (->{points}) := {self.rvalue}({self.rvalue.type})" return f"{lvalue}({lvalue.type}) (->{points}) := {self.rvalue}({rvalue_type})"
return f"{lvalue}({lvalue.type}) := {self.rvalue}({self.rvalue.type})"
return f"{lvalue}({lvalue.type}) := {self.rvalue}({rvalue_type})"

@ -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;
}
}

@ -7,6 +7,7 @@ from crytic_compile.platform.solc_standard_json import SolcStandardJson
from slither import Slither from slither import Slither
from slither.printers.inheritance.inheritance_graph import PrinterInheritanceGraph from slither.printers.inheritance.inheritance_graph import PrinterInheritanceGraph
from slither.printers.summary.slithir import PrinterSlithIR
TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data" TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data"
@ -34,3 +35,18 @@ def test_inheritance_printer(solc_binary_path) -> None:
assert counter["B -> A"] == 2 assert counter["B -> A"] == 2
assert counter["C -> A"] == 1 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"]

Loading…
Cancel
Save