MISRAC2004-1.2_f
In this section:
Synopsis
(Required) No reliance shall be placed on undefined or unspecified behavior.
Enabled by default
Yes
Severity/Certainty
Low/High

Full description
A variable used as a divisor is subsequently compared with 0. This check is identical to ATH-div-0-cmp-bef, MISRAC2012-Rule-1.3_d, SEC-DIV-0-compare-before, CERT-INT33-C_c.
Coding standards
- CERT INT33-C
Ensure that division and modulo operations do not result in divide-by-zero errors
- CWE 369
Divide By Zero
- 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;
}