More strict rules on variable type

pull/62/head
Josselin 6 years ago
parent 9b818aab16
commit 82a5461ed9
  1. 6
      slither/core/variables/variable.py
  2. 7
      slither/slithir/convert.py
  3. 3
      slither/slithir/operations/binary.py
  4. 5
      slither/slithir/variables/tuple.py

@ -3,7 +3,8 @@
""" """
from slither.core.source_mapping.source_mapping import SourceMapping from slither.core.source_mapping.source_mapping import SourceMapping
from slither.core.solidity_types.type import Type
from slither.core.solidity_types.elementary_type import ElementaryType
class Variable(SourceMapping): class Variable(SourceMapping):
@ -73,6 +74,9 @@ class Variable(SourceMapping):
return self._visibility return self._visibility
def set_type(self, t): def set_type(self, t):
if isinstance(t, str):
t = ElementaryType(t)
assert isinstance(t, Type) or t is None
self._type = t self._type = t
def __str__(self): def __str__(self):

@ -189,6 +189,9 @@ def convert_to_low_level(ir):
call = SolidityFunction('abi.{}()'.format(ir.function_name)) call = SolidityFunction('abi.{}()'.format(ir.function_name))
new_ir = SolidityCall(call, ir.nbr_arguments, ir.lvalue, ir.type_call) new_ir = SolidityCall(call, ir.nbr_arguments, ir.lvalue, ir.type_call)
new_ir.arguments = ir.arguments new_ir.arguments = ir.arguments
if isinstance(call.return_type, list) and len(call.return_type) == 1:
new_ir.lvalue.set_type(call.return_type[0])
else:
new_ir.lvalue.set_type(call.return_type) new_ir.lvalue.set_type(call.return_type)
return new_ir return new_ir
elif ir.function_name in ['call', 'delegatecall', 'callcode']: elif ir.function_name in ['call', 'delegatecall', 'callcode']:
@ -427,7 +430,7 @@ def propagate_types(ir, node):
if return_type: if return_type:
if len(return_type) == 1: if len(return_type) == 1:
ir.lvalue.set_type(return_type[0]) ir.lvalue.set_type(return_type[0])
else: elif len(return_type)>1:
ir.lvalue.set_type(return_type) ir.lvalue.set_type(return_type)
else: else:
ir.lvalue = None ir.lvalue = None
@ -483,7 +486,7 @@ def propagate_types(ir, node):
return_type = ir.function.return_type return_type = ir.function.return_type
if len(return_type) == 1: if len(return_type) == 1:
ir.lvalue.set_type(return_type[0]) ir.lvalue.set_type(return_type[0])
else: elif len(return_type)>1:
ir.lvalue.set_type(return_type) ir.lvalue.set_type(return_type)
elif isinstance(ir, TypeConversion): elif isinstance(ir, TypeConversion):
ir.lvalue.set_type(ir.type) ir.lvalue.set_type(ir.type)

@ -2,6 +2,7 @@ import logging
from slither.slithir.operations.lvalue import OperationWithLValue from slither.slithir.operations.lvalue import OperationWithLValue
from slither.core.variables.variable import Variable from slither.core.variables.variable import Variable
from slither.slithir.utils.utils import is_valid_lvalue, is_valid_rvalue from slither.slithir.utils.utils import is_valid_lvalue, is_valid_rvalue
from slither.core.solidity_types import ElementaryType
logger = logging.getLogger("BinaryOperationIR") logger = logging.getLogger("BinaryOperationIR")
@ -135,7 +136,7 @@ class Binary(OperationWithLValue):
self._type = operation_type self._type = operation_type
self._lvalue = result self._lvalue = result
if BinaryType.return_bool(operation_type): if BinaryType.return_bool(operation_type):
result.set_type('bool') result.set_type(ElementaryType('bool'))
else: else:
result.set_type(left_variable.type) result.set_type(left_variable.type)

@ -1,6 +1,7 @@
from slither.core.variables.variable import Variable from slither.core.variables.variable import Variable
from slither.core.solidity_types.type import Type
class TupleVariable(Variable): class TupleVariable(Variable):
COUNTER = 0 COUNTER = 0
@ -22,5 +23,9 @@ class TupleVariable(Variable):
def name(self): def name(self):
return 'TUPLE_{}'.format(self.index) return 'TUPLE_{}'.format(self.index)
def set_type(self, t):
assert all(isinstance(x, Type) or x is None for x in t)
self._type = t
def __str__(self): def __str__(self):
return self.name return self.name

Loading…
Cancel
Save