The product divides a value by zero.
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.
1000
699
Weakness
ChildOf
682
711
Category
ChildOf
730
734
Category
ChildOf
738
734
Category
ChildOf
739
844
Category
ChildOf
848
868
Category
ChildOf
872
868
Category
ChildOf
873
888
Category
ChildOf
885
Implementation
Medium
Availability
DoS: crash / exit /
restart
A Divide by Zero results in a crash.
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.
Java
```
public int computeAverageResponseTime (int totalTime, int
numRequests) {
```

`return totalTime / 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.
```
public int computeAverageResponseTime (int totalTime, int
numRequests) throws ArithmeticException {
```

`if (numRequests == 0) {`

`System.out.println("Division by zero attempted!");`

`throw ArithmeticException;`

`}`

`return totalTime / numRequests;`

`}`

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.
C
C++
`double divide(double x, double y){`

`return x/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.
`const int DivideByZero = 10;`

`double divide(double x, double y){`

`if ( 0 == y ){`

`throw DivideByZero;`

`}`

`return x/y;`

`}`

`...`

`try{`

`divide(10, 0);`

`}`

`catch( int i ){`

`if(i==DivideByZero) {`

`cerr<<"Divide by zero error";`

`}`

`}`

http://www.cprogramming.com/tutorial/exceptions.html
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.
C#
`int Division(int x, int y){`

`return (x / y);`

`}`

The method can be modified to raise, catch and handle the
DivideByZeroException if the input value used as the denominator is
zero.
`int SafeDivision(int x, int y){`

`try{`

`return (x / y);`

`}`

`catch (System.DivideByZeroException dbz){`

```
System.Console.WriteLine("Division by zero
attempted!");
```

`return 0;`

`}`

`}`

Microsoft Corporation
http://msdn.microsoft.com/en-us/library/ms173160(VS.80).aspx
CVE-2007-3268
Invalid size value leads to divide by
zero.
CVE-2007-2723
"Empty" content triggers divide by
zero.
CVE-2007-2237
Height value of 0 triggers divide by
zero.
Denial of Service
A9
CWE_More_Specific
Detect and handle floating point errors
FLP03-C
Ensure that division and modulo operations do not result in
divide-by-zero errors
INT33-C
Ensure that division and modulo operations do not result in
divide-by-zero errors
NUM02-J
Ensure that division and modulo operations do not result in
divide-by-zero errors
INT33-CPP
Detect and handle floating point errors
FLP03-CPP
Sean Eidemiller
Cigital
2008-07-01
added/updated demonstrative
examples
Eric Dalci
Cigital
2008-07-01
updated Time_of_Introduction
CWE Content Team
MITRE
2008-09-08
updated Common_Consequences, Description, Relationships,
Other_Notes, Taxonomy_Mappings
CWE Content Team
MITRE
2008-11-24
updated Relationships,
Taxonomy_Mappings
CWE Content Team
MITRE
2009-05-27
updated Demonstrative_Examples
CWE Content Team
MITRE
2009-10-29
updated Other_Notes
CWE Content Team
MITRE
2011-06-01
updated Common_Consequences, Relationships,
Taxonomy_Mappings
CWE Content Team
MITRE
2011-09-13
updated Relationships,
Taxonomy_Mappings
CWE Content Team
MITRE
2012-05-11
updated Relationships,
Taxonomy_Mappings