CertC-MSC21

Use robust loop termination conditions

Required inputs: IR, StaticSemanticAnalysis

C defines < , > , <= , and >= to be relational operators , and it defines == and != to be equality operators .

If a for or while statement uses a loop counter, than it is safer to use a relational operator (such as < ) to terminate the loop than to use an equality operator (such as != ).

Noncompliant Code Example (Equality Operators)

This noncompliant code example appears to have five iterations, but in fact, the loop never terminates:

size_t i;
for (i = 1; i != 10; i += 2) {
  /* ... */
}

Compliant Solution (Relational Operators)

Using the relational operator <= instead of an equality operator guarantees loop termination:

size_t i;
for (i = 1; i <= 10; i += 2 ) {
  /* ... */
}

Noncompliant Code Example (Equality Operators)

It is also important to ensure termination of loops where the start and end values are variables that might not be properly ordered. The following function assumes that begin < end ; if this is not the case, the loop will never terminate:

void f(size_t begin, size_t end) {
  size_t i;
  for (i = begin; i != end; ++i) {
    /* ... */
  }
}

Compliant Solution (Relational Operators)

Again, using a relational operator instead of equivalence guarantees loop termination. If begin >= end , the loop never executes its body.

void f(size_t begin, size_t end) {
  size_t i;
  for (i = begin; i < end; ++i) {
    /* ... */
  }
}

Noncompliant Code Example (Boundary Conditions)

Numerical comparison operators do not always ensure loop termination when comparing against the minimum or maximum representable value of a type, such as SIZE_MAX :

void f(size_t begin, size_t step) {
  size_t i;
  for (i = begin; i <= SIZE_MAX; i += step) {
    /* ... */
  }
}

Compliant Solution (Boundary Conditions)

A compliant solution is to compare against the difference between the maximum representable value of a type and the increment:

void f(size_t begin, size_t step) {
  if (0 < step) {
    size_t i;
    for (i = begin; i <= SIZE_MAX - step; i += step) {
      /* ... */
    }
  }
}

Exceptions

MSC21-C-EX1: If the loop counter is incremented by 1 on each iteration, and it is known that the starting value of a loop is less than or equal to the ending value, then an equality operator may be used to terminate the loop. Likewise, if the loop counter is decremented by 1 on each iteration, and it is known that the starting value of the loop is greater than or equal to the ending value, then an equality operator may be used to terminate the loop.

size_t i;
for (i = 1; i != 5; ++i) {
  /* ... */
}
Risk Assessment

Testing for exact values runs the risk of a loop terminating much longer than expected or never terminating at all.

Recommendation Severity Likelihood Detectable Repairable Priority Level
MSC21-C Low Unlikely No No P1 L3
Related Guidelines
SEI CERT C++ Coding Standard VOID MSC21-CPP. Use inequality to terminate a loop whose counter changes by more than one
CERT Oracle Secure Coding Standard for Java MSC54-J. Avoid inadvertent wrapping of loop counters
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/miscellaneous-msc/msc21-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

loop_cond_true_type_limits

Loop condition is always true due to limited range of data type

None

False

stepping_loop_uses_equality_check

Loop-counter shall not be tested with equality operator if not modified by – or ++

None

False

unit_step_loop_nonconstant_bound

Loop-counter shall not be tested with equality operator against a non-constant value.

None

False

Options

ignore_literal_subexpressions

ignore_literal_subexpressions : bool = True

Whether literal operands of short-circuit operators inside conditions, as in assert(false && "message"), are exempt from subexpression_true/subexpression_false findings. Literal operands outside conditions (for example in assigned values) are reported regardless, as the subexpression finding is the only report there.
 

ignore_short_circuited_constants

ignore_short_circuited_constants : bool = True

Whether never-evaluated operands that are themselves constant, such as the message string in assert(false && "message"), are exempt from unreachable_short_circuit findings.
 

report_do_while_false

report_do_while_false : bool = True

Whether do ... while(0) should be reported.
 

report_for_true

report_for_true : bool = True

Whether for(;;) ... should be reported.
 

report_while_true

report_while_true : bool = True

Whether while(1) ... should be reported.