I'm not sure, why Misra-C report this error in the below snap of the code.
int count = 0;
element = TAILQ_LAST(&(phdata->ph_consice_head), phqueue);
for (;
element; // Malformed for-loop Condition help
The termination clause of this for loop is not of the expected form.
element = TAILQ_PREV(element, phqueue, next)) {
count ;
}
Note - element is pointer to a struct.
If someone can help me to figure out what's wrong with the for-loop that would be appreciable.
CodePudding user response:
MISRA is very much a creature of habits and does not like anything "looking unusual".
It likes for
loops best as in
for(something=value;
boolean_expression /* ideally something<size */;
incremental_change /* ideally something )
CodePudding user response:
MISRA C:2012 rule 14.2 is pretty self-explanatory regarding what it allows in the respective 3 clauses of a for
loop. Like with any rule, you need to study it before you can use MISRA C.
In this case the 2nd clause needs to use the loop iterator and the static analyser might be a bit confused regarding what to consider as the loop iterator here.
Furthermore element;
is non-compliant against MISRA 14.4, you need to explicitly make an "essentially boolean" expression such as element != NULL
.
For MISRA compliance I would recommend a rewrite like this:
element = TAILQ_LAST(&(phdata->ph_consice_head), phqueue);
for (int count = 0; ; count )
{
if(element == NULL)
{
break;
}
element = TAILQ_PREV(element, phqueue, next);
}
Alternatively you could also make the 2nd clause count<n
if there is some known maximum number of iterations.