Skip to content

rust.unbounded_recursion

Stability High

Detects recursive functions without depth limits that can cause stack overflow.

Unbounded recursion:

  • Stack overflow — Program crashes
  • No recovery — SIGSEGV is fatal
  • Hard to debug — Happens deep in call stack
// ❌ Before (unbounded recursion)
fn traverse(node: &Node) {
process(node);
for child in &node.children {
traverse(child); // No depth limit!
}
}
// ✅ After (with depth limit)
fn traverse(node: &Node, depth: usize) -> Result<(), Error> {
if depth > 100 {
return Err(Error::MaxDepthExceeded);
}
process(node);
for child in &node.children {
traverse(child, depth + 1)?;
}
Ok(())
}
  • Recursive functions without base case
  • Missing depth counters
  • Self-referential data traversal

Unfault adds depth parameters and checks.