CWE-369: Divide By Zero

Description

The product divides a value by zero.

Submission Date :

April 11, 2008, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

This weakness typically occurs when an unexpected value is provided to the product, or if an error occurs that is not properly detected. It frequently occurs in calculations involving physical dimensions such as size, length, width, and height.

Example Vulnerable Codes

Example - 1

The following Java example contains a function to compute an average but does not validate that the input value used as the denominator is not zero. This will create an exception for attempting to divide by zero. If this error is not handled by Java exception handling, unexpected results can occur.

return totalTime / numRequests;public int computeAverageResponseTime (int totalTime, int numRequests) {}

By validating the input value used as the denominator the following code will ensure that a divide by zero error will not cause unexpected results. The following Java code example will validate the input value, output an error message, and throw an exception.


System.out.println("Division by zero attempted!");throw ArithmeticException;
if (numRequests == 0) {}return totalTime / numRequests;public int computeAverageResponseTime (int totalTime, int numRequests) throws ArithmeticException {}

Example - 2

The following C/C++ example contains a function that divides two numeric values without verifying that the input value used as the denominator is not zero. This will create an error for attempting to divide by zero, if this error is not caught by the error handling capabilities of the language, unexpected results can occur.

return x/y;double divide(double x, double y){}

By validating the input value used as the denominator the following code will ensure that a divide by zero error will not cause unexpected results. If the method is called and a zero is passed as the second argument a DivideByZero error will be thrown and should be caught by the calling block with an output message indicating the error.


throw DivideByZero;
if ( 0 == y ){}return x/y;
divide(10, 0);
cerr<<"Divide by zero error";if(i==DivideByZero) {}const int DivideByZero = 10;double divide(double x, double y){}...try{}catch( int i ){}

Example - 3

The following C# example contains a function that divides two numeric values without verifying that the input value used as the denominator is not zero. This will create an error for attempting to divide by zero, if this error is not caught by the error handling capabilities of the language, unexpected results can occur.

return (x / y);int Division(int x, int y){}

The method can be modified to raise, catch and handle the DivideByZeroException if the input value used as the denominator is zero.

return (x / y);

System.Console.WriteLine("Division by zero attempted!");return 0;try{}catch (System.DivideByZeroException dbz){}int SafeDivision(int x, int y){}

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.

© cvefeed.io
Latest DB Update: Nov. 22, 2024 3:29