Detect Infinite Loop Program

Aidan Dyga, Molly Suppo, Javier Bejarano-Jimenez

September 8, 2025

Introduction

  • Goal: Implement DetectInfiniteLoop to flag source code that will never terminate.
  • Motivation: Infinite loops waste computational resources.
  • Examples of loops:
    • while(True)
    • for i in range(10)

What is an Infinite Loop?

  • Definition: Code that repeats forever without reaching a stopping point.
  • Key signs: No reachable exit condition, exit is impossible due to logic, waiting for an event that never happens
  • Common causes: Loop variable never changes, unreachable return statement, waiting for an event that never occurs

Is this Problem Tractable, Intractable, or Uncomputable?

  • Answer: Uncomputable in the general case.
  • Halting Problem: It is mathematically proven that no algorithm can always decide whether an arbitrary program will halt or run forever.
  • In Practice: We can only detect some infinite loops using patterns and heuristics.

Solutions

  • Static Analyzer: A tool that examines source code without running it, searching for bugs, unreachable code, or infinite loops. (ex. Vulture)
  • How They Work: Use pattern matching and heuristics to flag suspicious code (e.g., while True, loops with constant conditions).
  • Our Approach: Implement our very own basic static analyzer.

Python Implementation

Limitations

  • Not complete: Our program cannot detect every possible infinite loop (Halting Problem).
  • False positives: Code may look infinite (e.g., while True) but actually breaks out later.
  • False negatives: Some infinite loops depend on logic that’s too complex to analyze (e.g., recursion, user input, random values).

More Limitations

  • Example False Negative:

    i = 1
    while i != 0:
        i = (i * 2) % 5

    Looks like it will end, but runs forever.
    Our program would incorrectly say: “No infinite loop detected.”

  • No runtime awareness: The analyzer only looks at code patterns — it does not execute the program.

  • Scope: Focuses only on simple patterns (while True, infinite iterators) and may miss more subtle cases.

Conclusion

  • Impossible: Detecting all infinite loops is impossible due to the Halting Problem.
  • Partial Solution: Heuristics can still catch many real bugs.
  • Takeaway: Use static analysis to flag obvious cases, but undecidable in general.

Sources

  • Course Textbook
  • GitHub Copilot
  • Microsoft Copilot