[previous] MISRA-C:2004  Rule  16.2:  (Required) [next] Functions shall not call themselves, either directly or indirectly.
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.

Example Code:


#include "misra.h"
#include "m2cmex.h"

static void test_1602a( S16 x );
static void test_1602b( S16 x );
static void test_1602c( S16 x );
static void test_1602d( S16 x );
static void test_1602e( S16 x );


extern S16 test_1602( void )
{
   test_1602a( 5 );
   test_1602e( 5 );
   return 1;
}

static void test_1602a( S16 x )
{
   if ( x > 0 )
   {
      --x;
      test_1602b( x );
   }
}

static void test_1602b( S16 x )
{
   if ( x > 0 )
   {
      --x;
      test_1602c( x );
   }
}

static void test_1602c( S16 x )
{
   if ( x > 0 )
   {
      --x;
      test_1602d( x );
   }
}

static void test_1602d( S16 x )
{
   if ( x > 0 )
   {
      --x;
      test_1602a( x );
   }
}

static void test_1602e( S16 x )
{
   if ( x > 0 )
   {
      --x;
      test_1602e( x );                /* MISRA Violation */
   }
}


QAC messages that encompass this guideline:

1520 Functions are indirectly recursive.
3670 Recursive call to function containing this call.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents