6.16 Using Shift Operations for Multiplication and Division [PIK]
6.16.1 Applicability to language
The vulnerability as described in ISO/IEC 24772-1 clause 6.16 exists in C++. C++ complicates the discussion in 24772-1 clause 6.16 as a result of the integral promotion (see clause 6.06 [FLC]). A left-shift on an operand that gets promoted can result in a value outside the operand’s unpromoted type’s range.
Not every use of a shift operator is a bit-shift due to operator overloading.
6.16.2 Avoidance mechanisms for language users
To avoid the vulnerability or mitigate its ill effects, C++ software developers can:
- use the avoidance mechanisms of ISO/IEC TR 24772-1:2019 clause 6.16 *Using Shift Operations for Multiplication and Division [PIK].
- For promoted operands, consider converting the result of a shift operation back to the original type of the left hand operand (say using a static cast or an assignment to an object of that type) before using the result in further operations.