Unconditional Recursion

ID

go.unconditional_recursion

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

reliability, suspicious

Description

Reports a plain function that calls itself unconditionally at the top level of its body, with no condition guarding the self-call.

Rationale

A recursion without a base case overflows the stack on the very first call. This is almost always a forgotten exit condition or a copy-paste mistake. A self-call guarded by an if, switch, for or select, or preceded by a return, can avoid recursing and is not reported.

// Bad
func f()          { f() }            // FLAW — calls itself unconditionally
func g(n int) int { return g(n) }    // FLAW — unconditional self-call in return

// Good
func h(n int) int {                  // OK — guarded by a base case
    if n <= 1 { return 1 }
    return n * h(n-1)
}

Remediation

Add a base case that ends the recursion, or guard the recursive call with a condition that is eventually false.