I have a very simple function which created a time delay:
void delay(int time_in_ms)
{
int t = get_time() + time_in_ms;
while (get_time() < t)
{
};
}
delay(750);
I'm getting the warning that the control variable t is not modified inside the while loop. I don't need to modify the variable, I just need to be inside the while loop as long as the condition is met. How to bypass this in a "nice" way?
The warning is created by MISRA static analysis tool. The full message:
"The control variable in this loop construct is never modified"
MISRA C:2012 Rules applicable to message 2467:
While you may have some knowledge about getTime()
, the static analyzer basically assumes that the return value of getTime()
might never trigger the loop end condition, thus the loop would potentially be infinite.
So what you need is an infinite loop that's accepted by the analyzer, combined with a break-condition within the loop body. As far as I can tell from a quick search, only for(;;)
is accepted as an infinite loop by MISRA.
for ( ; ; )
{
if (t <= get_time()) break;
}
This should not trigger a warning.