CPP: Extend the test.

This commit is contained in:
Geoffrey White
2019-10-14 10:04:24 +01:00
parent 75bf339a9b
commit 62625cc454
2 changed files with 15 additions and 2 deletions

View File

@@ -3,3 +3,4 @@
| bsc.cpp:10:10:10:33 | ... >= ... | Potential unsafe sign check of a bitwise operation. |
| bsc.cpp:18:10:18:28 | ... > ... | Potential unsafe sign check of a bitwise operation. |
| bsc.cpp:22:10:22:28 | ... < ... | Potential unsafe sign check of a bitwise operation. |
| bsc.cpp:34:10:34:21 | ... >= ... | Potential unsafe sign check of a bitwise operation. |

View File

@@ -7,7 +7,7 @@ bool is_bit_set_v2(int x, int bitnum) {
}
bool plain_wrong(int x, int bitnum) {
return (x & (1 << bitnum)) >= 0; // ???
return (x & (1 << bitnum)) >= 0; // GOOD (testing for `>= 0` is the logical negation of `< 0`, a negativity test) [FALSE POSITIVE]
}
bool is_bit24_set(int x) {
@@ -27,5 +27,17 @@ bool is_bit31_set_good(int x) {
}
bool deliberately_checking_sign(int x, int y) {
return (x & y) < 0; // GOOD (use of `<` implies the sign check is intended)
return (x & y) < 0; // GOOD (testing for negativity rather the positivity implies that signed values are being considered intentionally by the developer)
}
bool deliberately_checking_sign2(int x, int y) {
return (x & y) >= 0; // GOOD (testing for `>= 0` is the logical negation of `< 0`, a negativity test) [FALSE POSITIVE]
}
bool is_bit_set_v3(int x, int bitnum) {
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)
}
bool is_bit_set_v4(int x, int bitnum) {
return (x & (1 << bitnum)) >= 1; // BAD [NOT DETECTED]
}