Why is this an issue?
If the denominator to a division or modulo operation is zero it would result in a fatal error.
Noncompliant code example
void test_divide() {
int z = 0;
if (unknown()) {
// ..
z = 3;
} else {
// ..
}
z = 1 / z; // Noncompliant, possible division by zero
}
Compliant solution
void test_divide() {
int z = 0;
if (unknown()) {
// ..
z = 3;
} else {
// ..
z = 1;
}
z = 1 / z;
}
Resources
- MITRE, CWE-369 - Divide by zero
- CERT, NUM02-J. - Ensure that division and remainder operations do not result in
divide-by-zero errors
- CERT, INT33-C. - Ensure that division and remainder operations do not result in
divide-by-zero errors