Infinite Recursive Loop

ID

java.infinite_recursive_loop

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:674, reliability

Description

Reports methods that call themselves unconditionally, leading to infinite recursion and a guaranteed StackOverflowError.

Rationale

A recursive method without a base case will call itself indefinitely until the JVM runs out of stack space. This is always a bug.

// Bad -- no base case
public int factorial(int n) {
    return n * factorial(n - 1);
}

Remediation

Add a base case that terminates the recursion.

public int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}