Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Noncompliant Code Example

...

In this example the addition could result in overflow

Code Block
bgColor#FFcccc

public int do_operation(int a,int b)
{
   int temp = a + b;
//Could result in overflow
//do other processing
   return temp;
}



Compliant Solution (Bounds Checking)


A solution would be to explicitly check the range of each arithmetic operation and throw an ArithmeticException on overflow, otherwise downcast the value to an integer. For arithmetical operations on  really big numbers one should always use the BigInteger Class

 In this platform according to SUN Java Data Types:
  -the integer data type is a 32-bit signed two's complement integer. It has a minimum value of -2,

Compliant Solution (Bounds Checking)

A solution would be to explicitly check the range of each arithmetic operation and throw an ArithmeticException on overflow, otherwise downcast the value to an integer. For arithmetical operations on  really big numbers one should always use the BigInteger Class

 In this platform according to SUN Java Data Types:
  -the integer data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
  - the long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int 

So since long is guaranteed to be able to hold the result of an int addition, we could assign the result to a long and if the result is in the integer range we simply downcast. All of the tests would be the same as with signed integers in C since Java does not support unsigned numbers

,647 (inclusive).
  - the long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int 

So since long is guaranteed to be able to hold the result of an int addition, we could assign the result to a long and if the result is in the integer range we simply downcast. All of the tests would be the same as with signed integers in C since Java does not support unsigned numbers

e.g for the previous example

Code Block
bgColor#ccccff

public int do_operation(int a, int b) throws ArithmeticException

{
   long temp = (long)a+(long)b;
   if(temp >Integer.MAX_VALUE || temp < Integer.MIN_VALUE) throw ArithmeticException;
   else //Value within range can perform the addition
   //Do stuff
   return (int)temp;
}


Another example would be of explicit range checking would be: e.g for the previous example

Code Block
bgColor#ccccff
public int do_operation(int a, int b) throws ArithmeticException

{
     long temp = (long)a+(long)bint temp;
       if(temp >Integer.MAX_VALUE || temp < Integer.MIN_VALUE) throw ArithmeticException;
   else //Value within range can perform the addition
   //Do stuff
   return (int)temp;
}

...

Code Block
bgColor#ccccff

public int do_operation(int a, int b) throws ArithmeticException
{
a>0 && b>0 && (a >Integer.MAX_VALUE - b) || a<0 && b<0 && (a < Integer.MIN_VALUE -b))
              throw ArithmeticException;
       int temp;else
       if(a>0 && b>0 && (a >Integer.MAX_VALUE -temp b)= ||a a<0 && b<0 && (a < Integer.MIN_VALUE -b))+ b;//Value within range can perform the addition
      //Do stuff return
      throw ArithmeticException;
       else temp;
}


 Another compliant approach would be to use the BigInteger class in this example and in the examples of the other operations using a wrapper for test of the overflow:

Code Block
bgColor#ccccff

public bool overflow(int a, int b)
{
    java.math.BigInteger ba =       temp = a + b;//Value within range can perform the additionnew java.math.BigInteger(String.valueOf(a));
    java.math.BigInteger bb = new java.math.BigInteger(String.valueOf(b));
    java.math.BigInteger br //Do stuff return
       temp;
}

...

Code Block
bgColor#ccccff

public bool overflow(int a, int b)
{
    java.math.BigInteger ba = new java.math.BigInteger(String.valueOf(a));
    java.math.BigInteger bb = new java.math.BigInteger(String.valueOf(b));
    java.math.BigInteger br = ba.add(bb);
    if(br.compareTo(java.math.BigInteger.valueOf(Integer.MAX_VALUE)) == 1
      = ba.add(bb);
    if(br.compareTo(java.math.BigInteger.valueOf(Integer.MAX_VALUE)) == 1
              || br.compareTo(java.math.BigInteger.valueOf(Integer.MIN_VALUE))== -1)
        return true;//We have overflow
    //Can proceed
   return false
}

public int do_operation(int a, int b) throws ArithmeticException
{
      if overflow(a,b)
        || br.compareTo(java.math.BigInteger.valueOf(Integer.MIN_VALUE))== -1) throw ArithmeticException;
        returnelse true;//Wewe haveare overflow
within range safely perform //Can proceed
   return false
}
the addition
}


By using the BigInteger class there is no chance to overflow (see section on BigInteger class) but the performance is degraded so it should be used only on really large operations








Subtraction


Care must be taken in subtraction operations as well as these can overflow as well.

Noncompliant Code Example


Code Block
bgColor#FFcccc
public int do_operation(int a, int b) throws ArithmeticException
{
   int temp = if overflow(a,b)
         throw ArithmeticException;
      else //we are within range safely perform the addition
}

...

Subtraction

...

- b;
//Could result in overflow
//perform other processing
   return temp;
}



Compliant Code Example


Code Block
bgColor#ccccff
int a,b,result;

long temp = (long)a-(long)b;
if(long < Integer.MIN_VALUE || long > Integer.MAX_VALUE)
throw ArithmeticException;
else
result = (int) temp;

...


This noncompliant code example, can result in a signed integer overflow during the multiplication of the signed operands a and b. If this behaviour is unanticipated, the resulting value may lead to undefined behaviour

Noncompliant Code Example


 

Code Block
bgColor#FFcccc
int a,b,result
//do stuff
result = a*b;//May result in overflow

...

A non-compliant example is:

Noncompliant Code Example


 

Code Block
bgColor#FFcccc
int a,b,result
result = a/b;

...