From 41352f75807c1fe068028a0788b8744c11db221c Mon Sep 17 00:00:00 2001 From: Gary Feng <104392015+gfeng-certik@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:22:03 -0500 Subject: [PATCH] fix: keep information from AST about a TupleExpression being an inline array and generate SlithIR accordingly (#26) CertiKProject/slither-task#212 --- slither/core/expressions/tuple_expression.py | 10 ++++++++-- slither/solc_parsing/expressions/expression_parsing.py | 2 +- slither/visitors/slithir/expression_to_slithir.py | 8 +++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/slither/core/expressions/tuple_expression.py b/slither/core/expressions/tuple_expression.py index 1fd8fc7956..1515d84649 100644 --- a/slither/core/expressions/tuple_expression.py +++ b/slither/core/expressions/tuple_expression.py @@ -4,15 +4,21 @@ class TupleExpression(Expression): - def __init__(self, expressions: List[Expression]) -> None: + def __init__(self, expressions: List[Expression], is_inline_array: bool = False) -> None: assert all(isinstance(x, Expression) for x in expressions if x) super().__init__() self._expressions = expressions + self._is_inline_array = is_inline_array @property def expressions(self) -> List[Expression]: return self._expressions + @property + def is_inline_array(self) -> bool: + return self._is_inline_array + def __str__(self) -> str: expressions_str = [str(e) for e in self.expressions] - return "(" + ",".join(expressions_str) + ")" + l_bracket, r_bracket = ("[","]") if self._is_inline_array else ("(",")") + return l_bracket + ",".join(expressions_str) + r_bracket diff --git a/slither/solc_parsing/expressions/expression_parsing.py b/slither/solc_parsing/expressions/expression_parsing.py index 4991984ff7..f0c326f656 100644 --- a/slither/solc_parsing/expressions/expression_parsing.py +++ b/slither/solc_parsing/expressions/expression_parsing.py @@ -386,7 +386,7 @@ def parse_expression(expression: Dict, caller_context: CallerContextExpression) for idx, _ in enumerate(elems): if elems[idx] == "": expressions.insert(idx, None) - t = TupleExpression(expressions) + t = TupleExpression(expressions, is_inline_array=expression.get("isInlineArray", False)) t.set_offset(src, caller_context.compilation_unit) return t diff --git a/slither/visitors/slithir/expression_to_slithir.py b/slither/visitors/slithir/expression_to_slithir.py index 0f91f9bd16..8ade1aa866 100644 --- a/slither/visitors/slithir/expression_to_slithir.py +++ b/slither/visitors/slithir/expression_to_slithir.py @@ -700,7 +700,13 @@ def _post_new_elementary_type(self, expression: NewElementaryType) -> None: def _post_tuple_expression(self, expression: TupleExpression) -> None: all_expressions = [get(e) if e else None for e in expression.expressions] - if len(all_expressions) == 1: + if expression.is_inline_array: + temp_var = TemporaryVariable(self._node) + init_arr = InitArray(expressions, temp_var) + self._result.append(init_arr) + # Use the new temporary variable in place of the array. + val = temp_var + elif len(all_expressions) == 1: val = all_expressions[0] else: val = all_expressions