Divide By Zero

The product divides a value by zero.


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.

Demonstrations

The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

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.

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;
}

Example Two

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.

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";
  }
}

Example Three

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.

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;
  }
}

See Also

Comprehensive Categorization: Incorrect Calculation

Weaknesses in this category are related to incorrect calculation.

SEI CERT C Coding Standard - Guidelines 04. Integers (INT)

Weaknesses in this category are related to the rules and recommendations in the Integers (INT) section of the SEI CERT C Coding Standard.

SEI CERT Oracle Secure Coding Standard for Java - Guidelines 03. Numeric Types and Operations (NUM)

Weaknesses in this category are related to the rules and recommendations in the Numeric Types and Operations (NUM) section of the SEI CERT Oracle Secure Coding Standar...

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

CWE Cross-section

This view contains a selection of weaknesses that represent the variety of weaknesses that are captured in CWE, at a level of abstraction that is likely to be useful t...

Weaknesses Introduced During Implementation

This view (slice) lists weaknesses that can be introduced during implementation.


Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.