The analyzer has detected an arithmetic operation that may result in an overflow.
Look at the following 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 half the maximum value of System.Int32
. 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 is guaranteed 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;
}
....
}
This diagnostic is classified as: