>
>
>
V2594. MISRA. Controlling expressions s…


V2594. MISRA. Controlling expressions should not be invariant.

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guide.

This rule only applies to programs written in C. Controlling expressions in 'if', '?:', 'while', 'for', 'do', 'switch' should not be invariant, that is, controlling expressions should not always lead to executing the same code branch. An invariant value in a controlling expression may indicate a program error. The compiler may remove any code, unreachable due to an invariant expression. Expressions containing 'volatile' variables are not invariant.

Exceptions:

  • 'do' loops with a controlling expression of the essential 'Boolean' type, which is evaluated as '0';
  • invariants that are used to create infinite loops.

Note. The following invariants may be used to create infinite loops:

  • literals of the essential 'Boolean' type: '1' or 'true' (C99);
  • converting a constant literal '1' to an essential 'Boolean' type (for example, '(bool) 1');
  • 'for' loop without a controlling expression.

Consider an example:

void adjust(unsigned error)
{
  if (error < 0)
  {
    increase_value(-error);
  }
  else
  {
    decrease_value(error);
  }
}

This example illustrates the error. The condition is always false because the function receives an unsigned integer. As a result, the 'decrease_value' function is always called. The compiler may remove the code branch with the 'increase_value' function.

This diagnostic is classified as:

  • MISRA-C-14.3