Bitwise shifts include left-shift operations of the form shift-expression <<
additive-expression and right-shift operations of the form shift-expression >>
additive-expression. The standard integer promotions are first performed on the operands, each of which has an integer type. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined. (See undefined behavior 5148.)
Do not shift an expression by a negative number of bits or by a number greater than or equal to the precision of the promoted left operand. The precision of an integer type is the number of bits it uses to represent values, excluding any sign and padding bits. For unsigned integer types, the width and the precision are the same; whereas for signed integer types, the width is one greater than the precision. This rule uses precision instead of width because, in almost every case, an attempt to shift by a number of bits greater than or equal to the precision of the operand indicates a bug (logic error). A logic error is different from overflow, in which there is simply a representational deficiency. In general, shifts should be performed only on unsigned operands. (See INT13-C. Use bitwise operators only on unsigned operands.)
...
Tool | Version | Checker | Description | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Astrée |
| precision-shift-width | Fully checked | |||||||||||||||||
Axivion Bauhaus Suite |
| CertC-INT34 | Can detect shifts by a negative or an excessive number of bits and right shifts on negative values. | |||||||||||||||||
CodeSonar |
| LANG.ARITH.BIGSHIFT | Shift amount exceeds bit width | |||||||||||||||||
Compass/ROSE | Can detect violations of this rule. Unsigned operands are detected when checking for INT13-C. Use bitwise operators only on unsigned operands | |||||||||||||||||||
Coverity |
| BAD_SHIFT | Implemented | |||||||||||||||||
Cppcheck |
| shiftNegative, shiftTooManyBits | Context sensitive analysis | |||||||||||||||||
Cppcheck Premium |
| shiftNegative, shiftTooManyBits premium-cert-int34-c | Context sensitive analysis Warns whenever Cppcheck sees a negative shift for a POD expression (The warning for shifting too many bits is written only if Cppcheck has sufficient type information and you use --platform to specify the sizes of the standard types.) | |||||||||||||||||
ECLAIR |
| CC2.INT34 | Partially implemented | |||||||||||||||||
Helix QAC |
| C0499, C2790, C++2790, C++3003 DF2791, DF2792, DF2793 | ||||||||||||||||||
Klocwork |
| MISRA.SHIFT.RANGE.2012 | ||||||||||||||||||
LDRA tool suite |
| 51 S, 403 S, 479 S | Partially implemented | |||||||||||||||||
Parasoft C/C++test |
| CERT_C-INT34-a | Avoid incorrect shift operations | |||||||||||||||||
Polyspace Bug Finder |
| Checks for:
Rule partially covered. | PRQA QA-C | |||||||||||||||||
Include Page | PRQA QA-C_v | PRQA QA-C_v | 0499, 2790 [C], 2791 [D], | Partially implemented | PRQA QA-C++ | |||||||||||||||
Include Page | cplusplus:PRQA QA-C++_V | cplusplus:PRQA QA-C++_V | 2791, 2792, 2793, 3003, 3321, 3322||||||||||||||||||
PVS-Studio |
| V610 | ||||||||||||||||||
RuleChecker |
| precision-shift-width-constant | Partially checked | |||||||||||||||||
TrustInSoft Analyzer |
| shift | Exhaustively verified (see one compliant and one non-compliant example). | Helix QAC | ||||||||||||||||
Include Page | Helix QAC_V | Helix QAC_V |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...