=20

** Name: **Use of extreme values in arithmetic expres=
sion

** Description: **If a variable is assigned the maxim=
um or minimum value for that variable's type and is then used in an arithme=
tic expression, this may result in an overflow.

** ID: **cpp/arithmetic-with-extreme-values

** Kind: **problem

** Severity: **warning

** Precision: **low

=20
##### Recommen=
dation

=20
##### Example

=20
##### References
=20

Assigning the maximum or minimum value for a type to a variable of that = type and then using the variable in calculations may cause overflows.

= =20Before using the variable, ensure that it is reassigned a value that doe= s not cause an overflow, or use a wider type to do the arithmetic.

=20In this example, assigning `INT_MAX`

to a variable and adding=
one causes an overflow. However, casting to a `long`

beforehand=
ensures that the arithmetic is done in the wider type, and so does not ove=
rflow.

```
1int main(int argc, char** argv)=
{ 2 int
```i =3D INT_MAX; 3 <=
/span>// BAD: overflow 4 int j =3D i=
+ 1; 5 6 // ... 7 8 int l =3D INT_MAX; 9 // GOOD: no overflow 10 long ~~k ~~=3D (long)l + 1; 11 }

=20