title | description | ms.date | f1_keywords | helpviewer_keywords | monikerRange | ||
---|---|---|---|---|---|---|---|
lnt-arithmetic-overflow |
Reference for Visual Studio C++ IntelliSense Linter check lnt-arithmetic-overflow. |
09/29/2021 |
|
|
>=msvc-160 |
An arithmetic expression may overflow before being converted to a wider type.
In C and C++, arithmetic operations are evaluated using the widest type of the operands, not the width of the type assigned the result. When a result is converted to a wider type, it indicates the developer expects the operation may overflow the narrower types of the operands.
The lnt-arithmetic-overflow
check is controlled by the Arithmetic Overflow setting in the C/C++ Code Style options. For information on how to change this setting, see Configure the linter.
#include <cstdint>
void overflow(int a, int b) {
int64_t mul = a * b; // Flagged: 32-bit operation may overflow.
int64_t shift = a << 34; // Flagged: Shift would overflow.
int64_t mul2 = mul + b; // OK: 'mul' is 64-bit so the addition expression is
// evaluated using 64-bit operations.
}
The fix the linter suggests is to explicitly widen one of the operands. Then the entire expression is evaluated at the wider result type, as shown in this example:
void overflow(int a, int b) {
int64_t mul = static_cast<int64_t>(a) * b;
int64_t shift = static_cast<int64_t>(a) << 34;
}