Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

Code Block
bgColor#ccccff
public int do_operation(int a, int b) throws ArithmeticException {
   long temp = (long)a + (long)b;
   if(temp >&gt; Integer.MAX_VALUE || temp <&lt; Integer.MIN_VALUE) {
     throw new ArithmeticException("&quot;Not in range"&quot;);
   }
   return (int)temp; // Value within range can perform the addition
}

...

Code Block
bgColor#ccccff
public int do_operation(int a, int b) throws ArithmeticException {
  if( b>0b&gt;0 ? a >&gt; Integer.MAX_VALUE - b : a <&lt; Integer.MIN_VALUE - b ) {
    throw new ArithmeticException("&quot;Not in range"&quot;);
  }
  return a + b;  //Value within range so addition can be performed
}

...

Code Block
bgColor#ccccff
public boolean overflow(long a, long b) {
  BigInteger ba = new BigInteger(String.valueOf(a));
  BigInteger bb = new BigInteger(String.valueOf(b));
  BigInteger br = ba.add(bb);
  return (br.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ||
          br.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) == -1);
}

public long do_operation(long a, long b) throws ArithmeticException {
  if(overflow(a,b)) {
    throw new ArithmeticException("&quot;Not in range"&quot;);
  }
  // Within range; safely perform the addition
  return a + b;
}

...

Code Block
bgColor#ccccff
public int do_operation(int a,int b) {
  long temp = (long)a - (long)b;
  if(temp <&lt; Integer.MIN_VALUE || temp >&gt; Integer.MAX_VALUE) {
    throw new ArithmeticException("&quot;Not in range"&quot;);
  }
  return (int) temp;
}

...

Code Block
bgColor#ccccff
public int do_operation(int a, int b) throws ArithmeticException {
  if(b >&gt; 0 ? a <&lt; Integer.MIN_VALUE + b : a >&gt; Integer.MAX_VALUE + b ) {
    throw new ArithmeticException("&quot;Not in range"&quot;);
  }
  return a - b;  //Value within range can perform the addition
}

...

Code Block
bgColor#ccccff
public boolean underflow(long a, long b) {
  BigInteger ba = new BigInteger(String.valueOf(a));
  BigInteger bb = new BigInteger(String.valueOf(b));
  BigInteger br = ba.subtract(bb);
  return (br.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ||
          br.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) == -1);
}

public long do_operation(long a, long b) throws ArithmeticException {
  if(underflow(a,b)) {
    throw new ArithmeticException("&quot;Not in range"&quot;);
  }
  // Within range; safely perform the subtraction
  return a - b;
}

...

Code Block
bgColor#ccccff
int a, b, result;
long temp = (long) a * (long)b;
if(temp >&gt; Integer.MAX_VALUE || temp <&lt; Integer.MIN_VALUE) {
  throw new ArithmeticException("&quot;Not in range"&quot;); // Overflow
}
result = (int) temp; // Value within range, safe to downcast

...

Code Block
bgColor#ccccff
if(a == Integer.MIN_VALUE &amp;&amp; b == -1) {
  throw new ArithmeticException("&quot;Not in range"&quot;); // May be Integer.MIN_VALUE and -1
}
result = a/b; // Safe operation

...

Code Block
bgColor#ccccff
if(result == Integer.MIN_VALUE) {
  throw new ArithmeticException("&quot;Not in range"&quot;);
}
temp = -result;

Absolute Value

...

Code Block
bgColor#FFcccc
int i = 0;
while ((-1 <<&lt;&lt; i) != 0)
  i++;

Compliant Solution

...

Code Block
bgColor#ccccff
for (int val = -1; val != 0; val <<&lt;&lt;= 1) { /* ... */ }

Risk Assessment

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

This rule appears in the C Secure Coding Standard as INT32-C. Ensure that operations on signed integers do not result in overflow.

...

Wiki Markup
\[[SCG 07|AA. Java References#SCG 07]\] Introduction
\[[JLS 03|AA. Java References#JLS 03]\] 4.2.2 Integer Operations and 15.22 Bitwise and Logical Operators
\[[Tutorials 08|AA. Java References#Tutorials 08]\] Primitive Data Types
\[[Seacord 05|AA. Java References#Seacord 05]\] Chapter 5. Integers
\[[Bloch 05|AA. Java References#Bloch 05]\] Puzzle 27: Shifty i's
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 682|http://cwe.mitre.org/data/definitions/682.html] "&quot;Incorrect Calculation"&quot;, [CWE ID 190|http://cwe.mitre.org/data/definitions/190.html] "&quot;Integer Overflow or Wraparound"&quot;, [CWE ID 191|http://cwe.mitre.org/data/definitions/191.html]  "&quot;Integer Underflow (Wrap or Wraparound)"&quot;

...

INT33-J. Do not cast numeric types to wider floating-point types without range checking      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;06. Integers (INT)      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INT35-J. Do not attempt to store signed values in the char integral type