Skip to content

Commit 10b6935

Browse files
committed
C++: Fix flow from this by ref.
1 parent 08b63d4 commit 10b6935

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowUtil.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ private newtype TNode =
1919
TInstanceParameterNode(MemberFunction f) { exists(f.getBlock()) and not f.isStatic() } or
2020
TPreConstructorInitThis(ConstructorFieldInit cfi) or
2121
TPostConstructorInitThis(ConstructorFieldInit cfi) or
22+
TThisArgumentPostUpdate(ThisExpr ta) {
23+
exists(Call c, int i |
24+
ta = c.getArgument(i) and
25+
not c.getTarget().getParameter(i).getUnderlyingType().(PointerType).getBaseType().isConst()
26+
)
27+
} or
2228
TUninitializedNode(LocalVariable v) { not v.hasInitializer() }
2329

2430
/**
@@ -282,6 +288,16 @@ private class PartialDefinitionNode extends PostUpdateNode, TPartialDefinitionNo
282288
override string toString() { result = getPreUpdateNode().toString() + " [post update]" }
283289
}
284290

291+
private class ThisArgumentPostUpdateNode extends PostUpdateNode, TThisArgumentPostUpdate {
292+
ThisExpr thisExpr;
293+
294+
ThisArgumentPostUpdateNode() { this = TThisArgumentPostUpdate(thisExpr) }
295+
296+
override Node getPreUpdateNode() { result.asExpr() = thisExpr }
297+
298+
override string toString() { result = "ref arg this" }
299+
}
300+
285301
/**
286302
* A node representing the temporary value of an object that was just
287303
* constructed by a constructor call or an aggregate initializer. This is only

cpp/ql/test/library-tests/dataflow/fields/by_reference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void test_setIndirectly() {
6060
void test_setThroughNonMember() {
6161
S s;
6262
s.setThroughNonMember(user_input());
63-
sink(s.getThroughNonMember()); // flow [NOT DETECTED]
63+
sink(s.getThroughNonMember()); // flow
6464
}
6565

6666
void test_nonMemberSetA() {

cpp/ql/test/library-tests/dataflow/fields/flow.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ edges
115115
| by_reference.cpp:56:3:56:3 | s [post update] [a] | by_reference.cpp:57:8:57:8 | s [a] |
116116
| by_reference.cpp:56:19:56:28 | call to user_input | by_reference.cpp:56:3:56:3 | s [post update] [a] |
117117
| by_reference.cpp:57:8:57:8 | s [a] | by_reference.cpp:57:10:57:22 | call to getIndirectly |
118+
| by_reference.cpp:62:3:62:3 | s [post update] [a] | by_reference.cpp:63:8:63:8 | s [a] |
119+
| by_reference.cpp:62:25:62:34 | call to user_input | by_reference.cpp:62:3:62:3 | s [post update] [a] |
120+
| by_reference.cpp:63:8:63:8 | s [a] | by_reference.cpp:63:10:63:28 | call to getThroughNonMember |
118121
| complex.cpp:34:15:34:15 | b [f, a_] | complex.cpp:44:8:44:8 | b [f, a_] |
119122
| complex.cpp:34:15:34:15 | b [f, b_] | complex.cpp:45:8:45:8 | b [f, b_] |
120123
| complex.cpp:44:8:44:8 | b [f, a_] | complex.cpp:44:10:44:10 | f [a_] |
@@ -203,6 +206,7 @@ edges
203206
| aliasing.cpp:93:12:93:13 | m1 | aliasing.cpp:92:12:92:21 | call to user_input | aliasing.cpp:93:12:93:13 | m1 | m1 flows from $@ | aliasing.cpp:92:12:92:21 | call to user_input | call to user_input |
204207
| by_reference.cpp:51:10:51:20 | call to getDirectly | by_reference.cpp:50:17:50:26 | call to user_input | by_reference.cpp:51:10:51:20 | call to getDirectly | call to getDirectly flows from $@ | by_reference.cpp:50:17:50:26 | call to user_input | call to user_input |
205208
| by_reference.cpp:57:10:57:22 | call to getIndirectly | by_reference.cpp:56:19:56:28 | call to user_input | by_reference.cpp:57:10:57:22 | call to getIndirectly | call to getIndirectly flows from $@ | by_reference.cpp:56:19:56:28 | call to user_input | call to user_input |
209+
| by_reference.cpp:63:10:63:28 | call to getThroughNonMember | by_reference.cpp:62:25:62:34 | call to user_input | by_reference.cpp:63:10:63:28 | call to getThroughNonMember | call to getThroughNonMember flows from $@ | by_reference.cpp:62:25:62:34 | call to user_input | call to user_input |
206210
| complex.cpp:44:12:44:12 | call to a | complex.cpp:55:13:55:22 | call to user_input | complex.cpp:44:12:44:12 | call to a | call to a flows from $@ | complex.cpp:55:13:55:22 | call to user_input | call to user_input |
207211
| complex.cpp:44:12:44:12 | call to a | complex.cpp:57:13:57:22 | call to user_input | complex.cpp:44:12:44:12 | call to a | call to a flows from $@ | complex.cpp:57:13:57:22 | call to user_input | call to user_input |
208212
| complex.cpp:45:12:45:12 | call to b | complex.cpp:56:13:56:22 | call to user_input | complex.cpp:45:12:45:12 | call to b | call to b flows from $@ | complex.cpp:56:13:56:22 | call to user_input | call to user_input |

0 commit comments

Comments
 (0)