CERT-INT33-C_i
In this section:
Synopsis
Ensure that division and remainder operations do not result in divide-by-zero errors.
Enabled by default
Yes
Severity/Certainty
Low/High

Full description
The result of the / operator is the quotient from the division of the first arithmetic operand by the second arithmetic operand. Division operations are susceptible to divide-by-zero errors. Overflow can also occur during two's complement signed integer division when the dividend is equal to the minimum (most negative) value for the signed integer type and the divisor is equal to -1.
Coding standards
- CERT INT33-C
Ensure that division and modulo operations do not result in divide-by-zero errors
Code examples
The following code example fails the check and will give a warning:
int foo(void)
{
int a = 3;
a--;
return 5 / (a-2); // a-2 is 0
}
The following code example passes the check and will not give a warning about this issue:
int foo(void)
{
int a = 3;
a--;
return 5 / (a+2); // OK - a+2 is 4
}