次の方法で共有


CA2233: Operations should not overflow

Item Value
RuleId 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.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

Don't perform arithmetic operations 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

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.

using System; 

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

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.

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

Output:

2147483647

Fix with Input Parameter Validation

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

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

Fix with a Checked Block

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.

Checked blocks are not supported in Visual Basic.

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