Misra C 2004 rule 13.6 (14.2 in the 2012 edition) says
Numeric variables being used within a for
loop for iteration counting shall not be modified in the body of the loop.
The code modifies i
in order to finish the for
loop (as the comment confirms). This is a violation of the rule.
Misra C 2004 rule 14.6 says:
For any iteration statement there shall be at most one break
statement used for loop termination.
Hence you can replace the offending code with a simple break
statement and still conform:
for (i = 0; i < FLASH; i++) {
if (name.see[i] == 0xFF) {
name.see[i] = faultId | mnemonicType;
break;
}
}
Yet Misra says you can only do this if there is a single break
statement in the loop. What if you want to test 2 different cases, handle them differently and break the loop on each of them. Using 2 break
statements seems an obvious choice, but for compliance you would need to add an extra variable do_break
, set it in the places where you want to break and test it just once at the end of the body to execute the break
statement. Not a very good practice IMHO...
Note these facts about Misra C coding standards:
Misra renumbered the rules from one edition to the next, a necessary change creating some confusion.
The rules are not available in open source. This would help spread some good practices, but arguably prevented some questionable ones.