Skip to content

Commit

Permalink
Merge pull request #118 from python-security/113_fix_Ifatty_raise_bug
Browse files Browse the repository at this point in the history
Fix the foddy infinite loop
  • Loading branch information
KevinHock authored Apr 17, 2018
2 parents 5dc7d39 + b3110b3 commit 687626b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pyt/stmt_visitor_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
import random
from collections import namedtuple

from .node_types import (
Expand Down Expand Up @@ -101,16 +102,24 @@ def get_first_node(
node,
node_not_to_step_past
):
"""
This is a super hacky way of getting the first node after a statement.
We do this because we visit a statement and keep on visiting and get something in return that is rarely the first node.
So we loop and loop backwards until we hit the statement or there is nothing to step back to.
"""
ingoing = None
i = 0
current_node = node
while current_node.ingoing:
# This is used because there may be multiple ingoing and loop will cause an infinite loop if we did [0]
i = random.randrange(len(current_node.ingoing))
# e.g. We don't want to step past the Except of an Except basic block
if current_node.ingoing[0] == node_not_to_step_past:
if current_node.ingoing[i] == node_not_to_step_past:
break
ingoing = current_node.ingoing
current_node = current_node.ingoing[0]
current_node = current_node.ingoing[i]
if ingoing:
return ingoing[0]
return ingoing[i]
return current_node


Expand Down

0 comments on commit 687626b

Please sign in to comment.