CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop')

Learn about CWE-835 (Loop with Unreachable Exit Condition ('Infinite Loop')), its security impact, exploitation methods, and prevention guidelines.

What is Loop with Unreachable Exit Condition ('Infinite Loop')?

• Overview: A Loop with Unreachable Exit Condition, also known as an Infinite Loop, occurs when a loop is written in such a way that its exit condition cannot be met, causing the program to run indefinitely. This often results from logical errors in the loop's condition or the failure to update loop control variables correctly.

• Exploitation Methods:

  • Attackers can exploit this vulnerability by causing a denial of service (DoS) where the program becomes unresponsive and consumes excessive resources.
  • Common attack patterns include manipulating input data or program state to trigger the infinite loop and cause system performance degradation.

• Security Impact:

  • Direct consequences include the application freezing or crashing, leading to unavailability for legitimate users.
  • Potential cascading effects include resource exhaustion on the host system, affecting other applications and services.
  • Business impact involves loss of service availability, potential revenue loss, and damage to reputation due to decreased user satisfaction.

• Prevention Guidelines:

  • Specific code-level fixes include ensuring that loop conditions can be met and incorporating proper break statements or exit logic.
  • Security best practices involve thorough testing, code reviews, and utilizing static analysis tools to detect unreachable exit conditions.
  • Recommended tools and frameworks include integrated development environments (IDEs) with debugging and code analysis features, and automated testing tools to simulate various input scenarios and ensure reliable loop termination.
Corgea can automatically detect and fix Loop with Unreachable Exit Condition ('Infinite Loop') in your codebase. [Try Corgea free today](https://corgea.app).

Technical Details

Likelihood of Exploit: Not specified

Affected Languages: Not Language-Specific

Affected Technologies: Not specified

Vulnerable Code Example

# This function attempts to process a list of numbers.
# The loop condition is incorrectly set, causing an infinite loop.
def process_numbers(numbers):
    i = 0
    # The loop condition is incorrect and causes the loop to run indefinitely
    while i <= len(numbers):  # The condition allows i to equal len(numbers), which is out of bounds
        print(numbers[i])  # This will eventually cause an IndexError
        i += 1

numbers = [1, 2, 3, 4, 5]
process_numbers(numbers)

How to fix Loop with Unreachable Exit Condition ('Infinite Loop')?

The infinite loop occurs due to the loop condition i <= len(numbers), which is flawed because it allows the loop to attempt accessing an index equal to the length of the list, which is out of bounds. To resolve this, the loop condition should be i < len(numbers), ensuring the loop only iterates over valid indices. This correction ensures that the loop exits after processing each element of the list, preventing an infinite loop.

Fixed Code Example

# Fixed the loop condition to properly exit after processing all elements.
def process_numbers(numbers):
    i = 0
    # The loop now correctly checks if i is less than the length of the list.
    while i < len(numbers):  # Correctly ensures i is always a valid index
        print(numbers[i])  # Safely accesses elements within bounds
        i += 1

numbers = [1, 2, 3, 4, 5]
process_numbers(numbers)

Explanation

  1. Vulnerable Code: The loop condition i <= len(numbers) is incorrect because it allows the loop to attempt accessing an index that is out of bounds, leading to an infinite loop and potential runtime error.

  2. Fixed Code: The loop condition is changed to i < len(numbers), which ensures the loop iterates only over valid indices of the list, thus preventing the infinite loop and potential runtime error.

  3. Best Practices: The fixed code example follows Python best practices by ensuring that list access is always within valid bounds, preventing errors and infinite loops. The comments provide clear explanations of the changes and their purpose.

Corgea Logo

Find this vulnerability and fix it with Corgea

Scan your codebase for CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop') and get remediation guidance

Start for free and no credit card needed.