Skip to content

Commit 9f2fdbb

Browse files
committed
C++: More tests for RedundantNullCheckSimple
1 parent 12084fc commit 9f2fdbb

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

cpp/ql/test/query-tests/Likely Bugs/RedundantNullCheckSimple/RedundantNullCheckSimple.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
void test1(int *p) {
1+
void test_simple_bad(int *p) {
22
int x;
33
x = *p;
44
if (p == nullptr) { // BAD
55
return;
66
}
77
}
88

9-
void test2(int *p) {
9+
void test_not_same_basic_block(int *p) {
1010
int x = *p;
1111
if (x > 100)
1212
return;
@@ -31,3 +31,41 @@ bool check_curslist(ContainsIntPtr *cip) {
3131
// an AliasedDefinition instruction.
3232
return *cip->intPtr != nullptr; // GOOD
3333
}
34+
35+
void test_no_single_dominator(int *p, bool b) {
36+
int x;
37+
if (b) {
38+
x = *p;
39+
} else {
40+
x = *p;
41+
}
42+
if (p == nullptr) { // BAD [NOT DETECTED]
43+
return;
44+
}
45+
}
46+
47+
int test_postdominator_same_bb(int *p) {
48+
int b = (p == nullptr); // BAD
49+
// This dereference is a postdominator of the null check, meaning that all
50+
// paths from the check to the function exit will pass through it.
51+
return *p + b;
52+
}
53+
54+
int test_postdominator(int *p) {
55+
int b = (p == nullptr); // BAD [NOT DETECTED]
56+
57+
if (b) b++; // This line breaks up the basic block
58+
59+
// This dereference is a postdominator of the null check, meaning that all
60+
// paths from the check to the function exit will pass through it.
61+
return *p + b;
62+
}
63+
64+
int test_inverted_logic(int *p) {
65+
if (p == nullptr) { // BAD [NOT DETECTED]
66+
// The check above should probably have been `!=` instead of `==`.
67+
return *p;
68+
} else {
69+
return 0;
70+
}
71+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| RedundantNullCheckSimple.cpp:4:7:4:7 | Load: p | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:3:7:3:8 | Load: * ... | dereferenced here |
22
| RedundantNullCheckSimple.cpp:13:8:13:8 | Load: p | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:10:11:10:12 | Load: * ... | dereferenced here |
3+
| RedundantNullCheckSimple.cpp:48:12:48:12 | Load: p | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:51:10:51:11 | Load: * ... | dereferenced here |

0 commit comments

Comments
 (0)