...
or a logical (unsigned) shift.
This Even though this noncompliant code example explicitly declares the operands to a right shift as unsigned (see INT13-C. Use bitwise operators only on unsigned operands), it fails to test whether the right operand is negative or is greater than or equal to the width of the promoted left operand, allowing undefined behavior.
Code Block | ||
---|---|---|
| ||
int si1 = /* initialized using untrusted data */; int si2 = /* initialized using untrusted data */; int sresult; unsigned int ui1 = /* initialized using untrusted data */; unsigned int ui2 = /* initialized using untrusted data */; unsigned int uresult; sresult = si1 >> si2; uresult = ui1 >> ui2; |
...
Compliant Solution (Right Shift)
...