Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.FUNC.RECUR

Recursive function.

MISRA C 2012 Rule 17.2: Functions shall not call themselves, either directly or indirectly

Category: Analysis

Analysis: Undecidable, System

Applies to: C90, C99

Rationale

Recursion carries with it the danger of exceeding available stack space, which can lead to a serious failure. Unless recursion is very tightly controlled, it is not possible to determine before execution what the worst-case stack usage could be.

MISRA-C 2004 Rule 16.2 (required): Functions shall not call themselves, either directly or indirectly.

Recursive function.

This means that recursive function calls cannot be used in safety-related systems. Recursion carries with it the danger of exceeding available stack space, which can be a serious error. Unless recursion is very tightly controlled, it is not possible to determine before execution what the worst-case stack usage could be.

MISRA-C++ 2008 Rule 7-5-4 (advisory): Functions should not call themselves, either directly or indirectly.

Rationale

Unbounded recursion is likely to lead to a stack over-flow and may impact system timings. This is also the case for an iterative algorithm.

Example

int32_t fn ( int32_t x )
{
   if ( x > 0 )
   {
      x = x * fn ( x — 1 ); // Non-compliant
   }
   return ( x );
}
// File1.cpp
int32_t fn_2 ( int32_t x )
{
   if ( x > 0 )
   {
      x = x * fn_3 ( x — 1 ); // Non-compliant
   }
   return ( x );
}
// File2.cpp
int32_t fn_3 ( int32_t x )
{
   if ( x == 0 )
   {
       x = x * fn_2 ( x — 1 ); // Non-compliant
   }
   return ( x );
}