CPP: Add test cases.

This commit is contained in:
Geoffrey White
2019-07-17 11:33:49 +01:00
parent f12c057826
commit aa368d8763
2 changed files with 22 additions and 4 deletions

View File

@@ -1,3 +1,5 @@
| bsc.cpp:2:10:2:32 | ... > ... | Potential unsafe sign check of a bitwise operation. |
| bsc.cpp:6:10:6:32 | ... > ... | Potential unsafe sign check of a bitwise operation. |
| 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:30:10:30:20 | ... < ... | Potential unsafe sign check of a bitwise operation. |

View File

@@ -1,15 +1,31 @@
bool is_bit_set_v1(int x, int bitnum) {
return (x & (1 << bitnum)) > 0;
return (x & (1 << bitnum)) > 0; // BAD
}
bool is_bit_set_v2(int x, int bitnum) {
return ((1 << bitnum) & x) > 0;
return ((1 << bitnum) & x) > 0; // BAD
}
bool plain_wrong(int x, int bitnum) {
return (x & (1 << bitnum)) >= 0;
return (x & (1 << bitnum)) >= 0; // ???
}
bool is_bit24_set(int x) {
return (x & (1 << 24)) > 0;
return (x & (1 << 24)) > 0; // GOOD (result will always be positive)
}
bool is_bit31_set_bad_v1(int x) {
return (x & (1 << 31)) > 0; // BAD
}
bool is_bit31_set_bad_v2(int x) {
return 0 < (x & (1 << 31)); // BAD [NOT DETECTED]
}
bool is_bit31_set_good(int x) {
return (x & (1 << 31)) != 0; // GOOD (uses `!=`)
}
bool deliberately_checking_sign(int x, int y) {
return (x & y) < 0; // GOOD (use of `<` implies the sign check is intended) [FALSE POSITIVE]
}