forked from openzfs/zfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CodeQL check for bitshift precedence
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @name Bit shift precedence issue | ||
* @description Detects bit shift operations where an arithmetic operation could be misinterpreted due to lack of parentheses, specifically when the left child is an arithmetic expression other than multiplication, or the right child is an arithmetic expression. | ||
* @kind problem | ||
* @severity warning | ||
* @id cpp/bitshift-precedence-issue | ||
*/ | ||
|
||
import cpp | ||
|
||
class NonParenthesizedArithmeticExpr extends Expr { | ||
NonParenthesizedArithmeticExpr() { | ||
( | ||
this instanceof AddExpr or | ||
this instanceof SubExpr or | ||
this instanceof MulExpr or | ||
this instanceof DivExpr or | ||
this instanceof RemExpr | ||
) and | ||
not exists(ParenthesisExpr pe | pe.getExpr() = this) | ||
} | ||
} | ||
|
||
from Expr bitShiftExpr, NonParenthesizedArithmeticExpr arithExpr | ||
where | ||
( | ||
bitShiftExpr instanceof LShiftExpr or | ||
bitShiftExpr instanceof RShiftExpr | ||
) and | ||
( | ||
( | ||
// Check if the left operand is a non-parenthesized arithmetic expression excluding multiplication | ||
arithExpr = bitShiftExpr.(LShiftExpr).getLeftOperand() and | ||
not arithExpr instanceof MulExpr | ||
) or | ||
( | ||
// Check if the right operand is a non-parenthesized arithmetic expression | ||
arithExpr = bitShiftExpr.(LShiftExpr).getRightOperand() | ||
) or | ||
( | ||
// Check if the left operand is a non-parenthesized arithmetic expression excluding multiplication | ||
arithExpr= bitShiftExpr.(RShiftExpr).getLeftOperand() and | ||
not arithExpr instanceof MulExpr | ||
) or | ||
( | ||
// Check if the right operand is a non-parenthesized arithmetic expression | ||
arithExpr = bitShiftExpr.(RShiftExpr).getRightOperand() | ||
) | ||
) | ||
select bitShiftExpr, | ||
"This bit shift operation's operand is an arithmetic operation without parentheses and may not be evaluated as intended due to operator precedence." |