CS.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# 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 CS.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 results in the failure of the process or an exception. It can also result in the success of the operation, but gives an erroneous answer.

Mitigation and prevention

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   namespace DBZ
2   {
3       class Program
4       {
5           static int Test1(int a, int size)
6           {
7               return a / size;
8           }
9           static void Main(string[] args)
10          {
11              int mean = Test1(0, 0);
12         }
13      }
14  }

Klocwork produces an issue report at line 11 indicating that the value '0' might be used in a division by zero by passing argument 2 to function 'Test1' at line 11.

Fixed code example

1   namespace DBZ
2   {
3       class Program
4       {
5           static int Test1(int a, int size)
6           {
7               if(size ==0)
8               { 
9                  return 0; 
10              }
11              return a / size;
12          }
13          static void Main(string[] args)
14          {
15              int mean = Test1(0, 0);
16         }
17      }
18  }

The issue from the vulnerable code example is fixed. The input variable 'size' is checked for a zero constant value in line 9 and prevents the division operation from occurring if the value is zero.

External guidance

Security training

Application security training materials provided by Secure Code Warrior.