Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loops in global scope #65

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

BergLucas
Copy link
Contributor

@BergLucas BergLucas commented Apr 8, 2024

Fix #63

Hi,

The problem is described in the referenced issue. I've fixed it by linking nodes doing infinite loops to the dummy exit node. Since the problem comes from an optimisation of Python, I wasn't sure whether indirect infinite loops could occur in the bytecode, so I dealt with that case too, just in case.

Example of an indirect infinite loop (even if I don't know if it's possible to create it except artificially):

Indirect infinite loop

If this can't happen, we could simplify the code and just link the nodes that are their only successors ({node} == cfg.get_successors(node)) to the dummy exit node.

The resulting augmented graph looks like that (to compare with the graph of the issue):

New augmented graph

I still wonder whether this is a good solution or not because I saw that there was an assertion that prevents code from being executed when it detects a "while true" loop with no exit node. The difference with this case is that the infinite loop only occurs under certain circumstances because of an if statement. If we block this kind of case with an assertion too, it will prevent testing the other branch of the if statement, so it's not perfect either. I've also noticed that the problem can occur in functions, so to fix the original issue, we would have needed to allow disabling the if __name__ == "__main__" as well as certain functions that have "while true" loops to fix that perfectly.

def foo(x: bool):
    if x:
        while True:
            pass

So what do you think, I'm not really an expert in this kind of graph but I hope my solution suits you. Don't hesitate if you have any comments or questions.

Have a nice day!

@stephanlukasczyk stephanlukasczyk merged commit c6e2884 into se2p:main Jun 21, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request option to ignore the if __name__ == '__main__' section of a module
2 participants