Skip to content

Commit

Permalink
fix: finish connecting true-statement node to end-if node before conn…
Browse files Browse the repository at this point in the history
…ecting condition node to false-statement node (#23)

- fixes the issue with wrong if-statement CFG when the then-clause is empty.

https://github.com/CertiKProject/slither-task/issues/241
  • Loading branch information
gfeng-certik authored Jan 27, 2023
1 parent 1be16a9 commit dda1275
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions slither/solc_parsing/declarations/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def _new_yul_block(

def _parse_if(self, if_statement: Dict, node: NodeSolc) -> NodeSolc:
# IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )?
falseStatement = None
false_body = None

if self.is_compact_ast:
condition = if_statement["condition"]
Expand All @@ -379,13 +379,9 @@ def _parse_if(self, if_statement: Dict, node: NodeSolc) -> NodeSolc:
trueStatement = self._parse_statement(
if_statement["trueBody"], condition_node, true_scope
)

if "falseBody" in if_statement and if_statement["falseBody"]:
false_scope = Scope(
node.underlying_node.scope.is_checked, False, node.underlying_node.scope
)
falseStatement = self._parse_statement(
if_statement["falseBody"], condition_node, false_scope
)
false_body = if_statement["falseBody"]
else:
children = if_statement[self.get_children("children")]
condition = children[0]
Expand All @@ -400,19 +396,22 @@ def _parse_if(self, if_statement: Dict, node: NodeSolc) -> NodeSolc:
node.underlying_node.scope.is_checked, False, node.underlying_node.scope
)
trueStatement = self._parse_statement(children[1], condition_node, true_scope)

if len(children) == 3:
false_scope = Scope(
node.underlying_node.scope.is_checked, False, node.underlying_node.scope
)
falseStatement = self._parse_statement(children[2], condition_node, false_scope)
false_body = children[2]

endIf_node = self._new_node(NodeType.ENDIF, if_statement["src"], node.underlying_node.scope)
link_underlying_nodes(trueStatement, endIf_node)

if falseStatement:
if false_body:
false_scope = Scope(
node.underlying_node.scope.is_checked, False, node.underlying_node.scope
)
falseStatement = self._parse_statement(false_body, condition_node, false_scope)
link_underlying_nodes(falseStatement, endIf_node)
else:
link_underlying_nodes(condition_node, endIf_node)

return endIf_node

def _parse_while(self, whilte_statement: Dict, node: NodeSolc) -> NodeSolc:
Expand Down

0 comments on commit dda1275

Please sign in to comment.