Skip to content

Commit

Permalink
Fix infinite loops in global scope
Browse files Browse the repository at this point in the history
  • Loading branch information
BergLucas committed Apr 8, 2024
1 parent c7e4419 commit ad2e368
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/pynguin/analyses/controlflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,16 @@ def _create_graph(
assert successor_node
cfg.add_edge(predecessor_node, successor_node, **attrs)

@staticmethod
def _infinite_loop_nodes(cfg: CFG) -> set[ProgramGraphNode]:
nodes: set[ProgramGraphNode] = set()
exit_nodes = cfg.exit_nodes
for node in cfg.nodes:
successors = cfg.get_successors(node)
if node in successors and successors.isdisjoint(exit_nodes):
nodes.add(node)
return nodes

@staticmethod
def _insert_dummy_entry_node(cfg: CFG) -> CFG:
dummy_entry_node = ProgramGraphNode(index=-1, is_artificial=True)
Expand All @@ -567,6 +577,8 @@ def _insert_dummy_exit_node(cfg: CFG) -> CFG:
cfg.add_node(dummy_exit_node)
for exit_node in exit_nodes:
cfg.add_edge(exit_node, dummy_exit_node)
for infinite_loop_node in CFG._infinite_loop_nodes(cfg):
cfg.add_edge(infinite_loop_node, dummy_exit_node)
return cfg

@property
Expand Down

0 comments on commit ad2e368

Please sign in to comment.