...
Code Block |
---|
|
void f(int begin, int end) {
int 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 INT_MIN
or INT_MAX
:
Code Block |
---|
|
void f(int begin, int step) {
int i;
for (i = begin; i <= INT_MAX; i += step) {
/* ... */
}
}
|
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 |
---|
|
void f(int begin, int step) {
if (0 < step) {
int i;
for (i = begin; i <= INT_MAX - step; i += step) {
/* ... */
}
}
}
|
Exceptions
...