CA2233: Operations should not overflow

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

TypeNameOperationsShouldNotOverflow
CheckIdCA2233
CategoryMicrosoft.Usage
Breaking ChangeNon Breaking

A method performs an arithmetic operation and does not validate the operands beforehand to prevent overflow.

Arithmetic operations should not be performed without first validating the operands to make sure that the result of the operation is not outside the range of possible values for the data types involved. Depending on the execution context and the data types involved, arithmetic overflow can result in either a System.OverflowException or the most significant bits of the result discarded.

To fix a violation of this rule, validate the operands before you perform the operation.

It is safe to suppress a warning from this rule if the possible values of the operands will never cause the arithmetic operation to overflow.

Description

A method in the following example manipulates an integer that violates this rule. Visual Basic requires the Remove integer overflow option to be disabled for this to fire.

Code

using System; 

namespace Samples
{    
    public static class Calculator    
    {        
        public static int Decrement(int input)        
        {             
            // Violates this rule            
            input--;             
            return input;        
        }    
    }
}

Comments

If the method in this example is passed T:System.Int32.MinValue, the operation would underflow. This causes the most significant bit of the result to be discarded. The following code shows how this occurs.

[C#]

public static void Main()  
{  
    int value = int.MinValue;    // int.MinValue is -2147483648   
    value = Calculator.Decrement(value);   
    Console.WriteLine(value);  
}  

[VB]

Public Shared Sub Main()       
    Dim value = Integer.MinValue    ' Integer.MinValue is -2147483648   
    value = Calculator.Decrement(value)   
    Console.WriteLine(value)   
End Sub  

Output

2147483647  

Description

The following example fixes the previous violation by validating the value of input.

Code

using System; 

namespace Samples
{    
    public static class Calculator    
    {        
        public static int Decrement(int input)        
        {            
            if (input == int.MinValue)                
                throw new ArgumentOutOfRangeException("input", "input must be greater than Int32.MinValue");
                             
            input--;             
            return input;        
        }    
    }
}

Description

The following example fixes the previous violation by wrapping the operation in a checked block. If the operation causes an overflow, a System.OverflowException will be thrown.

Note that checked blocks are not supported in Visual Basic.

Code

using System; 

namespace Samples
{    
    public static class Calculator    
    {        
        public static int Decrement(int input)        
        {            
            checked            
            {                
                input--;            
            }                        
            
            return input;        
        }    
    }
}

If you turn on checked arithmetic overflow/underflow in C#, it is equivalent to wrapping every integer operation in a checked block.

To turn on checked arithmetic overflow/underflow in C#

  1. In Solution Explorer, right-click your project and choose Properties.

  2. Select the Build tab and click Advanced.

  3. Select Check for arithmetic overflow/underflow and click OK.

System.OverflowException
C# Operators
Checked and Unchecked

Show: