Function calls itself
ID |
c.correctness.direct_recursion |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Control Flow |
Language |
C / C++ |
Description
This function calls itself. Recursion depth is bounded only at run time, so a deep or unbounded chain exhausts the stack — a failure mode that is hard to test for and fatal when it happens. Coding standards for safety-critical and embedded code forbid it outright. Rewrite the function iteratively, or establish and document a hard bound on the depth.
Rationale
This function calls itself. Recursion depth is bounded only at run time, so a deep or unbounded chain exhausts the stack — a failure mode that is hard to test for and fatal when it happens. Coding standards for safety-critical and embedded code forbid it outright. Rewrite the function iteratively, or establish and document a hard bound on the depth.
The following code illustrates the pattern detected by this rule:
/* Quality (correctness): a function that calls itself. */
int factorial(int n)
{
if (n <= 1) {
return 1;
}
// FLAGGED: Function calls itself
return n * factorial(n - 1);
}
Remediation
Follow secure coding practices and review the references below for detailed remediation guidance.