Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 687626b

Browse files
Merge pull request #118 from python-security/113_fix_Ifatty_raise_bug
Fix the foddy infinite loop
2 parents 5dc7d39 + b3110b3 commit 687626b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

‎pyt/stmt_visitor_helper.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
import random
23
from collections import namedtuple
34

45
from .node_types import (
@@ -101,16 +102,24 @@ def get_first_node(
101102
node,
102103
node_not_to_step_past
103104
):
105+
"""
106+
This is a super hacky way of getting the first node after a statement.
107+
We do this because we visit a statement and keep on visiting and get something in return that is rarely the first node.
108+
So we loop and loop backwards until we hit the statement or there is nothing to step back to.
109+
"""
104110
ingoing = None
111+
i = 0
105112
current_node = node
106113
while current_node.ingoing:
114+
# This is used because there may be multiple ingoing and loop will cause an infinite loop if we did [0]
115+
i = random.randrange(len(current_node.ingoing))
107116
# e.g. We don't want to step past the Except of an Except basic block
108-
if current_node.ingoing[0] == node_not_to_step_past:
117+
if current_node.ingoing[i] == node_not_to_step_past:
109118
break
110119
ingoing = current_node.ingoing
111-
current_node = current_node.ingoing[0]
120+
current_node = current_node.ingoing[i]
112121
if ingoing:
113-
return ingoing[0]
122+
return ingoing[i]
114123
return current_node
115124

116125

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /