Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3217. Possible overflow as a result...
menu mobile close menu
Additional information
toggle menu Contents

V3217. Possible overflow as a result of an arithmetic operation.

Feb 05 2025

The analyzer has detected an arithmetic operation that may result in an overflow.

The example:

private const int _halfMaximumValue = int.MaxValue / 2;

public void Calculate(int summand)
{
    int sum;

    if (summand > _halfMaximumValue + 1)
    {
        sum = _halfMaximumValue + summand;
    }

    ....
}

In the Calculate method, the sum of the passed parameter and the constant is calculated. The constant is equal to half of the maximum System.Int32 value. The parameter value is checked before the addition operation to avoid the arithmetic overflow.

However, the condition contains an error. In this case, there is a check whether summand is greater than _halfMaximumValue + 1. If the condition is true, the arithmetic overflow will occur during the addition operation.

For proper check execution, replace the > operator with <:

private const int _halfMaximumValue = int.MaxValue / 2;

public void Calculate(int summand)
{
    int sum;

    if (summand < _halfMaximumValue + 1)
    {
        sum = _halfMaximumValue + summand;
    }

    ....
}

Overflow can occur when using unverified external data as an operand:

private const int _halfMaximumValue = int.MaxValue / 2;

public void Calculate()
{
  var numberStr = Request.QueryString["number"];

  if (Int32.TryParse(numberStr, out int number))
  {
    int sum = _halfMaximumValue + number;
    ....
  }
}

The numberStr value, obtained from the external source, is converted to int and assigned to the number variable. Then, number is added to _halfMaximumValue. This may lead to an overflow if number is greater than half of the maximum int value.

To prevent this overflow issue, limit number before performing the addition:

private const int _halfMaximumValue = int.MaxValue / 2;

public void Calculate()
{
  var numberStr = Request.QueryString["number"];

  if (   Int32.TryParse(numberStr, out int number)
      && number < 1000) // <=
  {
    int sum = _halfMaximumValue + number;
    ....
  }
}

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
close form
Free PVS‑Studio license for Microsoft MVP specialists
close form
To get the licence for your open-source project, please fill out this form
close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam