Warning C26451
Arithmetic overflow: Using operator 'operator' on a size-a byte value and then casting the result to a size-b byte value. Cast the value to the wider type before calling operator 'operator' to avoid overflow (io.2)
This warning indicates incorrect behavior that results from integral promotion rules and types larger than the ones in which arithmetic is typically performed.
Remarks
Code analysis detects when an integral value gets shifted left, multiplied, added, or subtracted, and the result gets cast to a wider integral type. If the operation overflows the narrower integral type, then data is lost. You can prevent this loss by casting the value to a wider type before the arithmetic operation.
Code analysis name: RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE
Examples
The following code generates this warning:
void leftshift(int i) noexcept
{
unsigned long long x;
x = i << 31; // C26451 reported here
// code
}
To correct this warning, use the following code:
void leftshift(int i) noexcept
{
unsigned long long x;
x = static_cast<unsigned long long>(i) << 31; // OK
// code
}