Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect temporary variable type in NewArray operation #1777

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion slither/slithir/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,13 @@ def propagate_types(ir: Operation, node: "Node"): # pylint: disable=too-many-lo
if v:
ir.lvalue.set_type(v.type)
elif isinstance(ir, NewArray):
ir.lvalue.set_type(ir.array_type)
# Reconstruct the array type from base type and depth
# Note that the length of the array type is lost since NewArray expression does not store it.
# So, here we over-approximately set the length to None (dynamic array).
typ = ir.array_type
for _ in range(ir.depth):
typ = ArrayType(typ, None)
ir.lvalue.set_type(typ)
elif isinstance(ir, NewContract):
contract = node.file_scope.get_contract_from_name(ir.contract_name)
ir.lvalue.set_type(UserDefinedType(contract))
Expand Down
18 changes: 18 additions & 0 deletions tests/test_ssa_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Union, List, Optional

import pytest
from slither.core.solidity_types import ArrayType
from solc_select import solc_select
from solc_select.solc_select import valid_version as solc_valid_version

Expand Down Expand Up @@ -1077,3 +1078,20 @@ def test_issue_1748():
operations = f.slithir_operations
assign_op = operations[0]
assert isinstance(assign_op, InitArray)


def test_issue_1776():
source = """
contract Contract {
function foo() public returns (uint) {
uint[] memory arr = new uint[](2);
return 0;
}
}
"""
with slither_from_source(source) as slither:
c = slither.get_contract_from_name("Contract")[0]
f = c.functions[0]
operations = f.slithir_operations
assign_op = operations[0]
assert isinstance(assign_op.lvalue.type, ArrayType)