|
| 1 | +/** |
| 2 | + * @id cpp/misra/inappropriate-bitwise-or-shift-operands |
| 3 | + * @name RULE-7-0-4: The operands of bitwise operators and shift operators shall be appropriate |
| 4 | + * @description Bitwise and shift operators should only be applied to operands of appropriate types |
| 5 | + * and values to avoid implementation-defined or undefined behavior. |
| 6 | + * @kind problem |
| 7 | + * @precision very-high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-7-0-4 |
| 10 | + * scope/single-translation-unit |
| 11 | + * external/misra/enforcement/decidable |
| 12 | + * external/misra/obligation/required |
| 13 | + */ |
| 14 | + |
| 15 | +import cpp |
| 16 | +import codingstandards.cpp.misra |
| 17 | +import codingstandards.cpp.misra.BuiltInTypeRules |
| 18 | + |
| 19 | +predicate isSignedType(NumericType t) { t.getSignedness() = Signed() } |
| 20 | + |
| 21 | +predicate isUnsignedType(NumericType t) { t.getSignedness() = Unsigned() } |
| 22 | + |
| 23 | +predicate isConstantExpression(Expr e) { |
| 24 | + e instanceof Literal or |
| 25 | + e.isConstant() |
| 26 | +} |
| 27 | + |
| 28 | +predicate isValidShiftConstantRange(Expr right, Type leftType) { |
| 29 | + exists(int value | |
| 30 | + value = right.getValue().toInt() and |
| 31 | + value >= 0 and |
| 32 | + value < leftType.getSize() * 8 |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +predicate isSignedConstantLeftShiftException(LShiftExpr shift) { |
| 37 | + exists(Expr left, Expr right, NumericType leftType, int leftVal, int rightVal, int maxBit | |
| 38 | + left = shift.getLeftOperand() and |
| 39 | + right = shift.getRightOperand() and |
| 40 | + leftType = left.getType() and |
| 41 | + isConstantExpression(left) and |
| 42 | + isConstantExpression(right) and |
| 43 | + isSignedType(leftType) and |
| 44 | + isValidShiftConstantRange(right, leftType) and |
| 45 | + leftVal = left.getValue().toInt() and |
| 46 | + rightVal = right.getValue().toInt() and |
| 47 | + leftVal >= 0 and |
| 48 | + maxBit = leftType.getSize() * 8 - 1 and |
| 49 | + // Check that no set bit is shifted into or beyond the sign bit |
| 50 | + leftVal * 2.pow(rightVal) < 2.pow(maxBit) |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +class BinaryShiftOperation extends BinaryOperation { |
| 55 | + BinaryShiftOperation() { |
| 56 | + this instanceof LShiftExpr or |
| 57 | + this instanceof RShiftExpr |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class AssignShiftOperation extends AssignOperation { |
| 62 | + AssignShiftOperation() { |
| 63 | + this instanceof AssignLShiftExpr or |
| 64 | + this instanceof AssignRShiftExpr |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +from Expr x, string message |
| 69 | +where |
| 70 | + not isExcluded(x, ConversionsPackage::inappropriateBitwiseOrShiftOperandsQuery()) and |
| 71 | + ( |
| 72 | + // Binary bitwise operators (excluding shift operations) - both operands must be unsigned |
| 73 | + exists(BinaryBitwiseOperation op | |
| 74 | + not op instanceof BinaryShiftOperation and |
| 75 | + op = x and |
| 76 | + not ( |
| 77 | + isUnsignedType(op.getLeftOperand().getExplicitlyConverted().getType()) and |
| 78 | + isUnsignedType(op.getRightOperand().getExplicitlyConverted().getType()) |
| 79 | + ) and |
| 80 | + message = |
| 81 | + "Binary bitwise operator '" + op.getOperator() + "' requires both operands to be unsigned." |
| 82 | + ) |
| 83 | + or |
| 84 | + // Compound assignment bitwise operators - both operands must be unsigned |
| 85 | + exists(AssignBitwiseOperation op | |
| 86 | + not op instanceof AssignShiftOperation and |
| 87 | + op = x and |
| 88 | + not ( |
| 89 | + isUnsignedType(op.getLValue().getExplicitlyConverted().getType()) and |
| 90 | + isUnsignedType(op.getRValue().getExplicitlyConverted().getType()) |
| 91 | + ) and |
| 92 | + message = |
| 93 | + "Compound assignment bitwise operator '" + op.getOperator() + |
| 94 | + "' requires both operands to be unsigned." |
| 95 | + ) |
| 96 | + or |
| 97 | + // Bit complement operator - operand must be unsigned |
| 98 | + exists(ComplementExpr comp | |
| 99 | + comp = x and |
| 100 | + not isUnsignedType(comp.getOperand().getExplicitlyConverted().getType()) and |
| 101 | + message = "Bit complement operator '~' requires unsigned operand." |
| 102 | + ) |
| 103 | + or |
| 104 | + // Shift operators - left operand must be unsigned |
| 105 | + exists(BinaryShiftOperation shift | |
| 106 | + shift = x and |
| 107 | + not isUnsignedType(shift.getLeftOperand().getExplicitlyConverted().getType()) and |
| 108 | + not isSignedConstantLeftShiftException(shift) and |
| 109 | + message = "Shift operator '" + shift.getOperator() + "' requires unsigned left operand." |
| 110 | + ) |
| 111 | + or |
| 112 | + // Shift operators - right operand must be unsigned or constant in valid range |
| 113 | + exists(BinaryShiftOperation shift, Expr right | |
| 114 | + shift = x and |
| 115 | + shift = x and |
| 116 | + right = shift.getRightOperand() and |
| 117 | + not isUnsignedType(right.getExplicitlyConverted().getType()) and |
| 118 | + not isValidShiftConstantRange(right, shift.getLeftOperand().getExplicitlyConverted().getType()) and |
| 119 | + message = |
| 120 | + "Shift operator '" + shift.getOperator() + |
| 121 | + "' requires unsigned right operand or constant in valid range." |
| 122 | + ) |
| 123 | + ) |
| 124 | +select x, message |
0 commit comments