MISRA C++:2023 Rule 8.2.10
Description
Rule Definition
Functions shall not call themselves, either directly or indirectly. 1
Rationale
Variables local to a function are stored in the call stack. If a function calls itself directly or indirectly several times, the available stack space can be exceeded, causing serious failure. Unless the recursion is tightly controlled, it is difficult to determine the maximum stack space required.
As an exception, constexpr
functions that are called recursively only
within a core constant expression do not violate this rule.
Polyspace Implementation
The checker reports each function that calls itself, directly or indirectly. Even if several functions are involved in one recursion cycle, each function is individually reported.
You can calculate the total number of recursion cycles using the code complexity metric
Number of Recursions
. Note that unlike the checker, the metric also
considers implicit calls, for instance, to compiler-generated constructors during object
creation.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Expressions |
Category: Required |
Version History
Introduced in R2024b1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.