I have a question related to MISRA 2012 Rule 14.2 "A for loop shall be well-formed"
Conside below sample code :
int foo (int *ptr)
{
(*ptr)--;
return *ptr;
}
void main()
{
int a =20;
int i;
for (i=0; i< foo(&a) ; i++)
{
/*
<loop body>
*/
}
}
Here for line for (i=0; i< foo(&a) ; i++)
I am getting a MISRA violation, 14.2.
The question is when we modify the variable (a) present in the loop condition (i< foo(&a)), in a function like shown. is it valid violation ?
Its just a sample case, for 14.2, Please do not focus on the loop being infinite in the above sample code.
14.2 Rule :
Second clause which
- Shall be an expression that has no persistent side effects, and
- Shall use the loop counter and optionally loop control flags, and
- Shall not use any other object that is modified in the for loop body.
Example :-
bool_t flag = false;
for ( int16_t i = 0; ( i < 5 ) && !flag; i++ )
{
if ( C )
{
flag = true; /* Compliant - allows early termination
* of loop */
}
i = i + 3; /* Non-compliant - altering the loop
* counter */
}