Skip to content

Commit 2e7daf2

Browse files
committed
C++: Use GVN in RedundantNullCheckSimple
1 parent 6c267f4 commit 2e7daf2

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

cpp/ql/src/Likely Bugs/RedundantNullCheckSimple.ql

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
/*
1515
* Note: this query is not assigned a precision yet because we don't want it on
16-
* LGTM until its performance is well understood. It's also lacking qhelp.
16+
* LGTM until its performance is well understood.
1717
*/
1818

1919
import semmle.code.cpp.ir.IR
20+
import semmle.code.cpp.ir.ValueNumbering
2021

2122
class NullInstruction extends ConstantValueInstruction {
2223
NullInstruction() {
@@ -25,17 +26,6 @@ class NullInstruction extends ConstantValueInstruction {
2526
}
2627
}
2728

28-
/**
29-
* An instruction that will never have slicing on its result.
30-
*/
31-
class SingleValuedInstruction extends Instruction {
32-
SingleValuedInstruction() {
33-
this.getResultMemoryAccess() instanceof IndirectMemoryAccess
34-
or
35-
not this.hasMemoryResult()
36-
}
37-
}
38-
3929
predicate explicitNullTestOfInstruction(Instruction checked, Instruction bool) {
4030
bool = any(CompareInstruction cmp |
4131
exists(NullInstruction null |
@@ -56,17 +46,17 @@ predicate explicitNullTestOfInstruction(Instruction checked, Instruction bool) {
5646
)
5747
}
5848

59-
predicate candidateResult(LoadInstruction checked, SingleValuedInstruction sourceValue)
49+
predicate candidateResult(LoadInstruction checked, ValueNumber value)
6050
{
6151
explicitNullTestOfInstruction(checked, _) and
6252
not checked.getAST().isInMacroExpansion() and
63-
sourceValue = checked.getSourceValue()
53+
value.getAnInstruction() = checked
6454
}
6555

66-
from LoadInstruction checked, LoadInstruction deref, SingleValuedInstruction sourceValue
56+
from LoadInstruction checked, LoadInstruction deref, ValueNumber sourceValue
6757
where
6858
candidateResult(checked, sourceValue) and
69-
sourceValue = deref.getSourceAddress().(LoadInstruction).getSourceValue() and
59+
sourceValue.getAnInstruction() = deref.getSourceAddress() and
7060
// This also holds if the blocks are equal, meaning that the check could come
7161
// before the deref. That's still not okay because when they're in the same
7262
// basic block then the deref is unavoidable even if the check concluded that

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,34 @@ int test_inverted_logic(int *p) {
6969
return 0;
7070
}
7171
}
72+
73+
void test_indirect_local() {
74+
int a = 0;
75+
int *p = &a;
76+
int **pp = &p;
77+
int x;
78+
x = **pp;
79+
if (*pp == nullptr) { // BAD
80+
return;
81+
}
82+
}
83+
84+
void test_field_local(bool boolvar) {
85+
int a = 0;
86+
struct {
87+
int *p;
88+
} s = { &a };
89+
auto sp = &s;
90+
91+
if (boolvar) {
92+
int x = *sp->p;
93+
if (sp->p == nullptr) { // BAD
94+
return;
95+
}
96+
} else {
97+
int *x = sp->p;
98+
if (sp == nullptr) { // BAD [NOT DETECTED]
99+
return;
100+
}
101+
}
102+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
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 |
33
| 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 |
4+
| RedundantNullCheckSimple.cpp:79:7:79:9 | Load: * ... | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:78:7:78:10 | Load: * ... | dereferenced here |
5+
| RedundantNullCheckSimple.cpp:93:13:93:13 | Load: p | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:92:13:92:18 | Load: * ... | dereferenced here |

0 commit comments

Comments
 (0)