File tree Expand file tree Collapse file tree
src/Likely Bugs/Arithmetic
test/query-tests/Likely Bugs/Arithmetic/BitwiseSignCheck Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1212import cpp
1313
1414from RelationalOperation e , BinaryBitwiseOperation lhs
15- where lhs = e .getLeftOperand ( ) and
15+ where lhs = e .getGreaterOperand ( ) and
1616 lhs .getActualType ( ) .( IntegralType ) .isSigned ( ) and
1717 forall ( int op | op = lhs .( BitwiseAndExpr ) .getAnOperand ( ) .getValue ( ) .toInt ( ) | op < 0 ) and
18- e .getRightOperand ( ) .getValue ( ) = "0" and
18+ e .getLesserOperand ( ) .getValue ( ) = "0" and
1919 not e .isAffectedByMacro ( )
2020select e , "Potential unsafe sign check of a bitwise operation."
Original file line number Diff line number Diff line change 22| bsc.cpp:6:10:6:32 | ... > ... | Potential unsafe sign check of a bitwise operation. |
33| bsc.cpp:10:10:10:33 | ... >= ... | Potential unsafe sign check of a bitwise operation. |
44| bsc.cpp:18:10:18:28 | ... > ... | Potential unsafe sign check of a bitwise operation. |
5- | bsc.cpp:30 :10:30:20 | ... < ... | Potential unsafe sign check of a bitwise operation. |
5+ | bsc.cpp:22 :10:22:28 | ... < ... | Potential unsafe sign check of a bitwise operation. |
Original file line number Diff line number Diff line change @@ -19,13 +19,13 @@ bool is_bit31_set_bad_v1(int x) {
1919}
2020
2121bool is_bit31_set_bad_v2 (int x) {
22- return 0 < (x & (1 << 31 )); // BAD [NOT DETECTED]
22+ return 0 < (x & (1 << 31 )); // BAD
2323}
2424
2525bool is_bit31_set_good (int x) {
2626 return (x & (1 << 31 )) != 0 ; // GOOD (uses `!=`)
2727}
2828
2929bool deliberately_checking_sign (int x, int y) {
30- return (x & y) < 0 ; // GOOD (use of `<` implies the sign check is intended) [FALSE POSITIVE]
30+ return (x & y) < 0 ; // GOOD (use of `<` implies the sign check is intended)
3131}
You can’t perform that action at this time.
0 commit comments