Merge pull request #1306 from crytic/vladyan18-fix-ternary-with-type

Add check ElementaryTypeNameExpression in copy_expression
pull/1308/head
Feist Josselin 2 years ago committed by GitHub
commit 1a6976a0ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      slither/utils/expression_manipulations.py
  2. BIN
      tests/ast-parsing/compile/ternary-with-max.sol-0.8.15-compact.zip
  3. 5
      tests/ast-parsing/expected/ternary-with-max.sol-0.8.15-compact.json
  4. 7
      tests/ast-parsing/ternary-with-max.sol
  5. 1
      tests/test_ast_parsing.py

@ -9,6 +9,7 @@ from slither.core.expressions.assignment_operation import AssignmentOperation
from slither.core.expressions.binary_operation import BinaryOperation from slither.core.expressions.binary_operation import BinaryOperation
from slither.core.expressions.call_expression import CallExpression from slither.core.expressions.call_expression import CallExpression
from slither.core.expressions.conditional_expression import ConditionalExpression from slither.core.expressions.conditional_expression import ConditionalExpression
from slither.core.expressions.elementary_type_name_expression import ElementaryTypeNameExpression
from slither.core.expressions.expression import Expression from slither.core.expressions.expression import Expression
from slither.core.expressions.identifier import Identifier from slither.core.expressions.identifier import Identifier
from slither.core.expressions.index_access import IndexAccess from slither.core.expressions.index_access import IndexAccess
@ -80,7 +81,10 @@ class SplitTernaryExpression:
if isinstance(expression, ConditionalExpression): if isinstance(expression, ConditionalExpression):
raise SlitherException("Nested ternary operator not handled") raise SlitherException("Nested ternary operator not handled")
if isinstance(expression, (Literal, Identifier, IndexAccess, NewArray, NewContract)): if isinstance(
expression,
(Literal, Identifier, IndexAccess, NewArray, NewContract, ElementaryTypeNameExpression),
):
return return
# case of lib # case of lib

@ -0,0 +1,5 @@
{
"TernaryWithMax": {
"f(bool)": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->2;\n2[label=\"Node Type: IF 2\n\"];\n2->3[label=\"True\"];\n2->4[label=\"False\"];\n3[label=\"Node Type: EXPRESSION 3\n\"];\n3->5;\n4[label=\"Node Type: EXPRESSION 4\n\"];\n4->5;\n5[label=\"Node Type: END_IF 5\n\"];\n}\n"
}
}

@ -0,0 +1,7 @@
contract TernaryWithMax {
function f(
bool condition
) external returns(uint256 res) {
res = type(uint256).max / (condition ? 10 : 1) ;
}
}

@ -411,6 +411,7 @@ ALL_TESTS = [
Test("free_functions/libraries_from_free.sol", ["0.8.12"]), Test("free_functions/libraries_from_free.sol", ["0.8.12"]),
Test("free_functions/new_operator.sol", ["0.8.12"]), Test("free_functions/new_operator.sol", ["0.8.12"]),
Test("free_functions/library_constant_function_collision.sol", ["0.8.12"]), Test("free_functions/library_constant_function_collision.sol", ["0.8.12"]),
Test("ternary-with-max.sol", ["0.8.15"]),
] ]
# create the output folder if needed # create the output folder if needed
try: try:

Loading…
Cancel
Save