MISRAC++2008-5-8-1
Synopsis
(Required) The right hand operand of a shift operator shall lie between zero and one less than the width in bits of the underlying type of the left hand operand.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
Possible out-of-range shifts were found. This check is identical to ATH-shift-bounds, MISRAC2004-12.8, MISRAC2012-Rule-12.2.
Coding standards
- CERT INT34-C
Do not shift a negative number of bits or more bits than exist in the operand
- CWE 682
Incorrect Calculation
- MISRA C:2004 12.8
(Required) The right-hand operand of a shift operator shall lie between zero and one less than the width in bits of the underlying type of the left-hand operand.
- MISRA C:2012 Rule-12.2
(Required) The right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operand
Code examples
The following code example fails the check and will give a warning:
unsigned int foo(unsigned int x, unsigned int y)
{
int shift = 33; // too big
return 3U << shift;
}
The following code example passes the check and will not give a warning about this issue:
unsigned int foo(unsigned int x)
{
int y = 1; // OK - this is within the correct range
return x << y;
}