Skip to content

Right Shift Not Number

Right operands of shift operators should be integers

What does this mean ?

Numbers can be shifted with the << and >> operators, but the right operand of the operation needs to be an int or a type that has an implicit conversion to int. However, with dynamic, the compiler's type checking is turned off, so you can pass anything to a shift operator and have it compile.

What can happen ?

If the argument can't be converted to int at runtime, then a RuntimeBinderException will be raised.

Recommendation

The right operand of the operation needs to be an int or a type that has an implicit conversion to int

Sample Code

Vulnerable :

dynamic d = 5;
var x = d >> 5.4; // Noncompliant
x = d >> null; // Noncompliant
x <<= new object(); // Noncompliant

References