Search code examples
cfor-loopmisra

Rule 14.2 A for loop shall be well-formed for MISRA C 2012


Why my tool for analysis of the rule for MISRA report a possible violation for this for loop ? For example

for(; i != 0 ; i = f(r) ) { int a = 9 + i; }

Solution

  • According to MISRA C 2012 document, the third clause of a for statement shall be an expression whose only persistent side effect is to modify the value of the loop counter and it shall not use objects that are modified in the for loop body. These restrictions also apply to all functions called within for statement.

    From the code you provided, I can guess, that f(r) function call contains side effects, such as modification of global variables or r argument. All other requirement described in rule 14.2 text are met.