ATH-div-0-assign
In this section:
Synopsis
A variable is assigned the value 0, then used as a divisor.
Enabled by default
Yes
Severity/Certainty
High/High

Full description
A variable is assigned the value 0, then used as a divisor. This will cause a 'divide by zero' runtime error. This check is identical to MISRAC2004-1.2_d, MISRAC2012-Rule-1.3_b, CERT-INT33-C_a.
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(void)
{
int a = 20, b = 0, c;
c = a / b; /* Divide by zero */
return c;
}
The following code example passes the check and will not give a warning about this issue:
int foo(void)
{
int a = 20, b = 5, c;
c = a / b; /* b is not 0 */
return c;
}