From 3147396cbf1dcf7d783b40c381878ba66e4138e3 Mon Sep 17 00:00:00 2001 From: alpharush <0xalpharush@protonmail.com> Date: Mon, 11 Sep 2023 16:56:45 -0500 Subject: [PATCH] fix(convert): do not convert array type to elementary for `InitArray` (#2018) fix(convert): do not convert array type to elementary for `InitArray` --- slither/slithir/convert.py | 2 +- slither/slithir/operations/init_array.py | 2 +- slither/slithir/operations/new_array.py | 2 +- tests/unit/slithir/test_ssa_generation.py | 22 +++++++++++++++++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/slither/slithir/convert.py b/slither/slithir/convert.py index d40715c4f3..75900f176f 100644 --- a/slither/slithir/convert.py +++ b/slither/slithir/convert.py @@ -1857,7 +1857,7 @@ def convert_constant_types(irs: List[Operation]) -> None: if isinstance(ir.lvalue.type.type, ElementaryType): if ir.lvalue.type.type.type in ElementaryTypeInt: for r in ir.read: - if r.type.type not in ElementaryTypeInt: + if r.type.type.type not in ElementaryTypeInt: r.set_type(ElementaryType(ir.lvalue.type.type.type)) was_changed = True diff --git a/slither/slithir/operations/init_array.py b/slither/slithir/operations/init_array.py index e0754c7705..ec2a63ef00 100644 --- a/slither/slithir/operations/init_array.py +++ b/slither/slithir/operations/init_array.py @@ -44,4 +44,4 @@ def convert(elem): return f"{elem}({elem.type})" init_values = convert(self.init_values) - return f"{self.lvalue}({self.lvalue.type}) = {init_values}" + return f"{self.lvalue}({self.lvalue.type}) = {init_values}" diff --git a/slither/slithir/operations/new_array.py b/slither/slithir/operations/new_array.py index 39b989459f..917bb11ee9 100644 --- a/slither/slithir/operations/new_array.py +++ b/slither/slithir/operations/new_array.py @@ -33,4 +33,4 @@ def read(self) -> List["Constant"]: def __str__(self): args = [str(a) for a in self.arguments] lvalue = self.lvalue - return f"{lvalue}{lvalue.type}) = new {self.array_type}({','.join(args)})" + return f"{lvalue}({lvalue.type}) = new {self.array_type}({','.join(args)})" diff --git a/tests/unit/slithir/test_ssa_generation.py b/tests/unit/slithir/test_ssa_generation.py index 3c7e84973f..b8772ca612 100644 --- a/tests/unit/slithir/test_ssa_generation.py +++ b/tests/unit/slithir/test_ssa_generation.py @@ -11,7 +11,7 @@ from slither import Slither from slither.core.cfg.node import Node, NodeType from slither.core.declarations import Function, Contract -from slither.core.solidity_types import ArrayType +from slither.core.solidity_types import ArrayType, ElementaryType from slither.core.variables.local_variable import LocalVariable from slither.core.variables.state_variable import StateVariable from slither.slithir.operations import ( @@ -1116,3 +1116,23 @@ def test_issue_1846_ternary_in_ternary(slither_from_source): assert node.type == NodeType.IF assert node.son_true.type == NodeType.IF assert node.son_false.type == NodeType.EXPRESSION + + +def test_issue_2016(slither_from_source): + source = """ + contract Contract { + function test() external { + int[] memory a = new int[](5); + } + } + """ + with slither_from_source(source) as slither: + c = slither.get_contract_from_name("Contract")[0] + f = c.functions[0] + operations = f.slithir_operations + new_op = operations[0] + lvalue = new_op.lvalue + lvalue_type = lvalue.type + assert isinstance(lvalue_type, ArrayType) + assert lvalue_type.type == ElementaryType("int256") + assert lvalue_type.is_dynamic