CA2233: Operations should not overflow

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName OperationsShouldNotOverflow
CheckId CA2233
Category Microsoft.Usage
Breaking Change Non Breaking

Cause

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

Rule Description

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.

How to Fix Violations

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

When to Suppress Warnings

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.

Example of a Violation

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

Public Module Calculator     

    Public Function Decrement(ByVal input As Integer) As Integer
             
        ' Violates this rule        
        input = input - 1         
        Return input
             
    End Function 
    
End Module

Comments

If the method in this example is passed 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

Fix with Input Parameter Validation

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;        
        }    
    }
}
Public Module Calculator

    Public Function Decrement(ByVal input As Integer) As Integer

        If (input = Integer.MinValue) Then _
            Throw New ArgumentOutOfRangeException("input", "input must be greater than Int32.MinValue")

        input = input - 1
        Return input

    End Function

End Module

Fix with a Checked Block

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

Turn on Checked Arithmetic Overflow/Underflow

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.

See Also

System.OverflowException C# Operators Checked and Unchecked