Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

DBZ.CONST.CALL

Zero constant value is passed to a function and might be used in a division by zero

An attempt to do a division or modulo operation using zero as the divisor causes a runtime error. Division by zero defects often occur due to ineffective error handling or race conditions, and typically cause abnormal program termination. Before a value is used as the divisor of a division or modulo operation in C/C++ code, it must be checked to confirm that it is not equal to zero.

The DBZ checkers look for instances in which a zero constant value is used as the divisor of a division or modulo operation.

The DBZ.CONST.CALL checker flags situations in which an explicit zero constant value is passed directly to a function call and might be used as a divisor of a division or modulo operation without checking it for the zero value.

Vulnerability and risk

Integer division by zero usually result in the failure of the process or an exception. It can also result in success of the operation, but gives an erroneous answer. Floating-point division by zero is more subtle. It depends on the implementation of the compiler. If the compiler is following the IEEE floating-point standard (IEEE 754), then the result of the floating-point division by zero has a well-defined result. However, the C and C++ standards do not enforce compliance to IEEE 754. Thus, floating-point division by zero has an undefined behavior in C and C++ and might result in the failure of the process or an exception.

Division by zero issues typically occur due to ineffective exception handling. To avoid this vulnerability, check for a zero value before using it as the divisor of a division or modulo operation.

Vulnerable code example

1  int compute_mean(int array[], size_t size)
2  {
3      int sum = 0;
4      for (size_t i = 0; i < size; ++i) {
5          sum += array[i];
6      }
7      return sum / size;
8  }
9
10 void use_mean()
11 {     
12     int mean = compute_mean(0, 0);
13 }

Klocwork produces an issue report at line 11 indicating that the zero constant value is passed directly to the function ‘compute_mean’ and it is used in this function as the divisor of the division operation on line 7. A division by zero can produce unexpected and unintended results.

Fixed code example

1  int compute_mean(int array[], size_t size)
2  {
3      if (size == 0) {
4          return 0;   // or exceptional case.
5      }
6
7      int sum = 0;
8      for (size_t i = 0; i < size; ++i) {
9          sum += array[i];
10     }    
11     return sum / size;
12 } 
13
14 void use_mean()
15 {
16     int mean = compute_mean(0, 0);
17 } 

The problem from the previous snippet is fixed: the input variable ‘size’ is checked for the exceptional case of the zero constant value in line 3 and prevents the division from happening in this specific case.

External guidance

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information.

Limitations

This checker only deals with integral types, not floating-point types. This means that defects involving floating-point types will not be detected by this checker. For example:
1  void do_dbz(double divisor)
2  {    
3      double doit = 23 / divisor;     // Divide by zero will not be detected here since it is a floating-point type.
4  }
5
6  void call_do_dbz()
7  {   
8      do_dbz(0.0);    // Divide by zero not detected by this checker.
9  }