Skip to main content

IAR Embedded Workbench for RH850 3.20.x

ATH-div-0-cmp-aft

In this section:
Synopsis

After a successful comparison with 0, a variable is used as a divisor.

Enabled by default

No

Severity/Certainty

Medium/High

mediumhigh.png
Full description

A variable is successfully compared to 0, then used as a divisor. This will cause a 'divide by zero' runtime error. This check is identical to MISRAC2004-1.2_e, MISRAC2012-Rule-1.3_c, SEC-DIV-0-compare-after, CERT-INT33-C_b.

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:

#include <stdlib.h>
int foo(void)
{
  int a = 20;
  int p = rand();

  if (p == 0)   /* p is 0 */
    a = 34 / p;

  return a;
}

The following code example passes the check and will not give a warning about this issue:

#include <stdlib.h>
int foo(void)
{
  int a = 20;
  int p = rand();

  if (p != 0)   /* p is not 0 */
    a = 34 / p;

  return a;
}