CERT-INT33-C_c
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. This check is identical to ATH-div-0-cmp-bef, MISRAC2004-1.2_f, MISRAC2012-Rule-1.3_d, SEC-DIV-0-compare-before.
Coding standards
- CERT INT33-C
Ensure that division and modulo operations do not result in divide-by-zero errors
- MISRA C:2004 1.2
(Required) No reliance shall be placed on undefined or unspecified behavior.
- MISRA C:2012 Rule-1.3
(Required) There shall be no occurrence of undefined or critical unspecified behaviour
Code examples
The following code example fails the check and will give a warning:
int foo(int p)
{
int a = 20, b = 1;
b = a / p;
if (p == 0) // Checking the value of 'p' too late.
return 0;
return b;
}
The following code example passes the check and will not give a warning about this issue:
int foo(int p)
{
int a = 20, b;
if (p == 0)
return 0;
b = a / p; /* Here 'p' is non-zero. */
return b;
}