Skip to content

Commit 527ec4a

Browse files
authored
Merge pull request github#2122 from geoffw0/bitsign2
CPP: BitwiseSignCheck.ql fix
2 parents 53720a3 + 62311eb commit 527ec4a

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

change-notes/1.23/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following changes in version 1.23 affect C/C++ analysis in all applications.
1818
| Hard-coded Japanese era start date in call (`cpp/japanese-era/constructor-or-method-with-exact-era-date`) | Deprecated | This query has been deprecated. Use the new combined query Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) instead. |
1919
| Hard-coded Japanese era start date in struct (`cpp/japanese-era/struct-with-exact-era-date`) | Deprecated | This query has been deprecated. Use the new combined query Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) instead. |
2020
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | More correct results | This query now checks for the beginning date of the Reiwa era (1st May 2019). |
21+
| Sign check of bitwise operation (`cpp/bitwise-sign-check`) | Fewer false positive results | Results involving `>=` or `<=` are no longer reported. |
2122
| Too few arguments to formatting function (`cpp/wrong-number-format-arguments`) | Fewer false positive results | Fixed false positives resulting from mistmatching declarations of a formatting function. |
2223
| Too many arguments to formatting function (`cpp/too-many-format-arguments`) | Fewer false positive results | Fixed false positives resulting from mistmatching declarations of a formatting function. |
2324
| Unclear comparison precedence (`cpp/comparison-precedence`) | Fewer false positive results | False positives involving template classes and functions have been fixed. |

cpp/ql/src/Likely Bugs/Arithmetic/BitwiseSignCheck.ql

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ import cpp
1414

1515
from RelationalOperation e, BinaryBitwiseOperation lhs
1616
where
17-
lhs = e.getGreaterOperand() and
17+
// `lhs > 0` (or `0 < lhs`)
18+
// (note that `lhs < 0`, `lhs >= 0` or `lhs <= 0` all imply that the signedness of
19+
// `lhs` is understood, so should not be flagged).
20+
(e instanceof GTExpr or e instanceof LTExpr) and
21+
e.getGreaterOperand() = lhs and
22+
e.getLesserOperand().getValue() = "0" and
23+
// lhs is signed
1824
lhs.getActualType().(IntegralType).isSigned() and
25+
// if `lhs` has the form `x & c`, with constant `c`, `c` is negative
1926
forall(int op | op = lhs.(BitwiseAndExpr).getAnOperand().getValue().toInt() | op < 0) and
20-
e.getLesserOperand().getValue() = "0" and
27+
// exception for cases involving macros
2128
not e.isAffectedByMacro()
2229
select e, "Potential unsafe sign check of a bitwise operation."
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
| bsc.cpp:2:10:2:32 | ... > ... | Potential unsafe sign check of a bitwise operation. |
22
| bsc.cpp:6:10:6:32 | ... > ... | Potential unsafe sign check of a bitwise operation. |
3-
| bsc.cpp:10:10:10:33 | ... >= ... | Potential unsafe sign check of a bitwise operation. |
43
| bsc.cpp:18:10:18:28 | ... > ... | Potential unsafe sign check of a bitwise operation. |
54
| bsc.cpp:22:10:22:28 | ... < ... | Potential unsafe sign check of a bitwise operation. |

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/BitwiseSignCheck/bsc.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ bool is_bit_set_v2(int x, int bitnum) {
77
}
88

99
bool plain_wrong(int x, int bitnum) {
10-
return (x & (1 << bitnum)) >= 0; // ???
10+
return (x & (1 << bitnum)) >= 0; // GOOD (testing for `>= 0` is the logical negation of `< 0`, a negativity test)
1111
}
1212

1313
bool is_bit24_set(int x) {
@@ -27,5 +27,17 @@ bool is_bit31_set_good(int x) {
2727
}
2828

2929
bool deliberately_checking_sign(int x, int y) {
30-
return (x & y) < 0; // GOOD (use of `<` implies the sign check is intended)
30+
return (x & y) < 0; // GOOD (testing for negativity rather the positivity implies that signed values are being considered intentionally by the developer)
31+
}
32+
33+
bool deliberately_checking_sign2(int x, int y) {
34+
return (x & y) >= 0; // GOOD (testing for `>= 0` is the logical negation of `< 0`, a negativity test)
35+
}
36+
37+
bool is_bit_set_v3(int x, int bitnum) {
38+
return (x & (1 << bitnum)) <= 0; // GOOD (testing for `<= 0` is the logical negation of `> 0`, a positivity test, but the way it's written suggests the developer considers the value to be signed)
39+
}
40+
41+
bool is_bit_set_v4(int x, int bitnum) {
42+
return (x & (1 << bitnum)) >= 1; // BAD [NOT DETECTED]
3143
}

0 commit comments

Comments
 (0)