-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathDoNotUseABitwiseOperatorWithABooleanLikeOperand.ql
More file actions
55 lines (51 loc) · 1.75 KB
/
DoNotUseABitwiseOperatorWithABooleanLikeOperand.ql
File metadata and controls
55 lines (51 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @id c/cert/do-not-use-a-bitwise-operator-with-a-boolean-like-operand
* @name EXP46-C: Do not use a bitwise operator with a Boolean-like operand
* @description Using bitwise operators with unparenthesized Boolean-like operands may indicate a
* logic error.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/cert/id/exp46-c
* maintainability
* readability
* external/cert/severity/low
* external/cert/likelihood/likely
* external/cert/remediation-cost/low
* external/cert/priority/p9
* external/cert/level/l2
* coding-standards/baseline/style
* external/cert/obligation/rule
*/
import cpp
import codingstandards.c.cert
/**
* Holds if `op` is a bitwise AND, OR, or XOR expression
*/
predicate isBitwiseOperationPotentiallyAmbiguous(BinaryBitwiseOperation op) {
op instanceof BitwiseAndExpr or
op instanceof BitwiseOrExpr or
op instanceof BitwiseXorExpr
}
/**
* Holds if `e` is an unparenthesised boolean expression,
* relational operation, or equality operation.
*/
predicate isDisallowedBitwiseOperationOperand(Expr e) {
not e.isParenthesised() and
(
e.getFullyConverted().getUnderlyingType() instanceof BoolType or
e instanceof RelationalOperation or
e instanceof EqualityOperation
)
}
from Expr operand, Operation operation
where
not isExcluded(operation,
ExpressionsPackage::doNotUseABitwiseOperatorWithABooleanLikeOperandQuery()) and
isBitwiseOperationPotentiallyAmbiguous(operation) and
operand = operation.getAnOperand() and
isDisallowedBitwiseOperationOperand(operand)
select operation,
"Bitwise operator " + operation.getOperator() +
" performs potentially unintended operation on $@.", operand, "boolean operand"