Why this matters
Numeric overflows occur when values exceed the data type limits, leading to incorrect calculations. Use checked arithmetic where necessary.
Numeric overflows occur when values exceed the data type limits, leading to incorrect calculations. Use checked arithmetic where necessary.
Numeric overflows occur when values exceed the data type limits, leading to incorrect calculations. Use checked arithmetic where necessary.
Side-by-side examples engineers can pattern-match during review.
public int Transform(int value)
{
if (value <= 0)
{
return value;
}
int number = int.MaxValue;
return number + value; // Noncompliant
}public long Transform(int value)
{
if (value <= 0)
{
return value;
}
long number = int.MaxValue;
return number + value;
}public int Transform(int value)
{
if (value <= 0)
{
return value;
}
int number = int.MaxValue;
return number + value; // Noncompliant
}public long Transform(int value)
{
if (value <= 0)
{
return value;
}
long number = int.MaxValue;
return number + value;
}From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.