CERT-INT34-C_c
Synopsis
Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand.
Enabled by default
Yes
Severity/Certainty
Low/Low

Full description
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. 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. This check is identical to ATH-shift-neg.
Coding standards
- CERT INT34-C
Do not shift a negative number of bits or more bits than exist in the operand
Code examples
The following code example fails the check and will give a warning:
int example(int x) {
return -10 >> x;
}
The following code example passes the check and will not give a warning about this issue:
int example(int x) {
return 10 >> x;
}