Skip to content

Commit 62625cc

Browse files
committed
CPP: Extend the test.
1 parent 75bf339 commit 62625cc

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
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. |
55
| bsc.cpp:22:10:22:28 | ... < ... | Potential unsafe sign check of a bitwise operation. |
6+
| bsc.cpp:34:10:34:21 | ... >= ... | 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) [FALSE POSITIVE]
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) [FALSE POSITIVE]
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)