Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added a NCCE inspired by MSC15-J.

...

Code Block
bgColor#ccccff
void f(int begin, int end) {
  int i;
  for (i = begin; i < end; ++i) {
    /* ... */
  }
}

Anchor
nce_boundary
nce_boundary

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 INT_MIN or INT_MAX:

Code Block
bgColor#ffcccc

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

Anchor
cs_boundary
cs_boundary

Compliant Solution (boundary conditions)

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

Code Block
bgColor#ccccff

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

Anchor
MSC21-EX1
MSC21-EX1

Exceptions

...