Skip to content

Commit c18428f

Browse files
authored
Merge pull request #9785 from bdrodes/main
C++: Nullness.qll bug fixes
2 parents a0636ff + f7c4fa6 commit c18428f

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* `AnalysedExpr::isNullCheck` and `AnalysedExpr::isValidCheck` have been updated to handle variable accesses on the left-hand side of the the C++ logical and variable declarations in conditions.

cpp/ql/lib/semmle/code/cpp/controlflow/Nullness.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ predicate nullCheckExpr(Expr checkExpr, Variable var) {
4646
or
4747
exists(LogicalAndExpr op, AnalysedExpr child |
4848
expr = op and
49-
op.getRightOperand() = child and
49+
op.getAnOperand() = child and
5050
nullCheckExpr(child, v)
5151
)
5252
or
@@ -99,7 +99,7 @@ predicate validCheckExpr(Expr checkExpr, Variable var) {
9999
or
100100
exists(LogicalAndExpr op, AnalysedExpr child |
101101
expr = op and
102-
op.getRightOperand() = child and
102+
op.getAnOperand() = child and
103103
validCheckExpr(child, v)
104104
)
105105
or
@@ -169,7 +169,10 @@ class AnalysedExpr extends Expr {
169169
*/
170170
predicate isDef(LocalScopeVariable v) {
171171
this.inCondition() and
172-
this.(Assignment).getLValue() = v.getAnAccess()
172+
(
173+
this.(Assignment).getLValue() = v.getAnAccess() or
174+
this.(ConditionDeclExpr).getVariableAccess() = v.getAnAccess()
175+
)
173176
}
174177

175178
/**

cpp/ql/test/library-tests/controlflow/nullness/nullness.expected

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
| test.cpp:15:8:15:23 | call to __builtin_expect | test.cpp:5:13:5:13 | v | is not null | is valid |
88
| test.cpp:16:8:16:23 | call to __builtin_expect | test.cpp:5:13:5:13 | v | is null | is not valid |
99
| test.cpp:17:9:17:17 | ... && ... | test.cpp:5:13:5:13 | v | is not null | is valid |
10-
| test.cpp:18:9:18:17 | ... && ... | test.cpp:5:13:5:13 | v | is not null | is not valid |
10+
| test.cpp:18:9:18:17 | ... && ... | test.cpp:5:13:5:13 | v | is not null | is valid |
1111
| test.cpp:19:9:19:18 | ... && ... | test.cpp:5:13:5:13 | v | is null | is not valid |
12-
| test.cpp:20:9:20:18 | ... && ... | test.cpp:5:13:5:13 | v | is not null | is not valid |
12+
| test.cpp:20:9:20:18 | ... && ... | test.cpp:5:13:5:13 | v | is null | is not valid |
1313
| test.cpp:21:9:21:14 | ... = ... | test.cpp:5:13:5:13 | v | is null | is not valid |
1414
| test.cpp:21:9:21:14 | ... = ... | test.cpp:7:10:7:10 | b | is not null | is valid |
15-
| test.cpp:22:17:22:17 | b | test.cpp:7:10:7:10 | b | is not null | is valid |
15+
| test.cpp:22:9:22:14 | ... = ... | test.cpp:5:13:5:13 | v | is not null | is not valid |
16+
| test.cpp:22:9:22:14 | ... = ... | test.cpp:7:13:7:13 | c | is not null | is not valid |
17+
| test.cpp:22:17:22:17 | c | test.cpp:7:13:7:13 | c | is not null | is valid |
18+
| test.cpp:23:21:23:21 | x | test.cpp:23:14:23:14 | x | is not null | is valid |
19+
| test.cpp:24:9:24:18 | (condition decl) | test.cpp:5:13:5:13 | v | is not null | is not valid |
20+
| test.cpp:24:9:24:18 | (condition decl) | test.cpp:24:14:24:14 | y | is not null | is valid |

cpp/ql/test/library-tests/controlflow/nullness/nullness.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import cpp
22

33
from AnalysedExpr a, LocalScopeVariable v, string isNullCheck, string isValidCheck
44
where
5-
a.getParent() instanceof IfStmt and
65
v.getAnAccess().getEnclosingStmt() = a.getParent() and
76
(if a.isNullCheck(v) then isNullCheck = "is null" else isNullCheck = "is not null") and
87
(if a.isValidCheck(v) then isValidCheck = "is valid" else isValidCheck = "is not valid")

cpp/ql/test/library-tests/controlflow/nullness/test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ long __builtin_expect(long);
44

55
void f(int *v) {
66
int *w;
7-
bool b;
7+
bool b, c;
88

99
if (v) {}
1010
if (!v) {}
@@ -19,5 +19,7 @@ void f(int *v) {
1919
if (true && !v) {}
2020
if (!v && true) {}
2121
if (b = !v) {}
22-
if (b = !v; b) {}
22+
if (c = !v; c) {}
23+
if (int *x = v; x) {}
24+
if (int *y = v) {}
2325
}

0 commit comments

Comments
 (0)