From ad2e368c171e023275f491217f6809c5582e1562 Mon Sep 17 00:00:00 2001 From: Berg Lucas <55436804+BergLucas@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:17:39 +0200 Subject: [PATCH] Fix infinite loops in global scope --- src/pynguin/analyses/controlflow.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/pynguin/analyses/controlflow.py b/src/pynguin/analyses/controlflow.py index ebe10b1a..c5eb3408 100644 --- a/src/pynguin/analyses/controlflow.py +++ b/src/pynguin/analyses/controlflow.py @@ -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) @@ -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