Skip to content

Commit 8e6daaf

Browse files
committed
C++: Add DefinitionByReferenceNode.getParameter
This commits also adds a test that uses `getParameter`. The new tests demonstrate that support for array-to-pointer decay works, but we get data flow to the array rather than its contents.
1 parent 2bc0a8d commit 8e6daaf

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ class DefinitionByReferenceNode extends Node, TDefinitionByReferenceNode {
121121
override Location getLocation() { result = argument.getLocation() }
122122
/** Gets the argument corresponding to this node. */
123123
Expr getArgument() { result = argument }
124+
/** Gets the parameter through which this value is assigned. */
125+
Parameter getParameter() {
126+
exists(FunctionCall call, int i |
127+
argument = call.getArgument(i) and
128+
result = call.getTarget().getParameter(i)
129+
)
130+
}
124131
}
125132

126133
/**

cpp/ql/test/library-tests/dataflow/dataflow-tests/DataflowTestCommon.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class TestAllocationConfig extends DataFlow::Configuration {
1212
or
1313
source.asParameter().getName().matches("source%")
1414
or
15+
source.(DataFlow::DefinitionByReferenceNode).getParameter().getName().matches("ref_source%")
16+
or
1517
// Track uninitialized variables
1618
exists(source.asUninitialized())
1719
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,39 @@ void cleanedByMemcpy_blockvar(int clean1) {
455455
memcpy(&tmp, &clean1, sizeof tmp);
456456
sink(tmp); // clean
457457
}
458+
459+
void intRefSource(int &ref_source);
460+
void intPointerSource(int *ref_source);
461+
void intArraySource(int ref_source[], size_t len);
462+
463+
void intRefSourceCaller() {
464+
int local;
465+
intRefSource(local);
466+
sink(local); // tainted
467+
}
468+
469+
void intPointerSourceCaller() {
470+
int local;
471+
intPointerSource(&local);
472+
sink(local); // tainted
473+
}
474+
475+
void intPointerSourceCaller2() {
476+
int local[1];
477+
intPointerSource(local);
478+
sink(local); // tainted
479+
sink(*local); // clean
480+
}
481+
482+
void intArraySourceCaller() {
483+
int local;
484+
intArraySource(&local, 1);
485+
sink(local); // tainted
486+
}
487+
488+
void intArraySourceCaller2() {
489+
int local[2];
490+
intArraySource(local, 2);
491+
sink(local); // tainted
492+
sink(*local); // clean
493+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
| test.cpp:433:8:433:10 | tmp | test.cpp:430:48:430:54 | source1 |
3131
| test.cpp:440:8:440:10 | tmp | test.cpp:436:53:436:59 | source1 |
3232
| test.cpp:442:10:442:12 | tmp | test.cpp:436:53:436:59 | source1 |
33+
| test.cpp:466:8:466:12 | local | test.cpp:465:16:465:20 | ref arg local |
34+
| test.cpp:472:8:472:12 | local | test.cpp:471:20:471:25 | ref arg & ... |
35+
| test.cpp:478:8:478:12 | local | test.cpp:477:20:477:24 | ref arg local |
36+
| test.cpp:485:8:485:12 | local | test.cpp:484:18:484:23 | ref arg & ... |
37+
| test.cpp:491:8:491:12 | local | test.cpp:490:18:490:22 | ref arg local |
3338
| true_upon_entry.cpp:21:8:21:8 | x | true_upon_entry.cpp:17:11:17:16 | call to source |
3439
| true_upon_entry.cpp:29:8:29:8 | x | true_upon_entry.cpp:27:9:27:14 | call to source |
3540
| true_upon_entry.cpp:39:8:39:8 | x | true_upon_entry.cpp:33:11:33:16 | call to source |

cpp/ql/test/library-tests/dataflow/dataflow-tests/test_diff.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
| test.cpp:430:48:430:54 | test.cpp:433:8:433:10 | AST only |
1313
| test.cpp:436:53:436:59 | test.cpp:440:8:440:10 | AST only |
1414
| test.cpp:436:53:436:59 | test.cpp:442:10:442:12 | AST only |
15+
| test.cpp:465:16:465:20 | test.cpp:466:8:466:12 | AST only |
16+
| test.cpp:471:20:471:25 | test.cpp:472:8:472:12 | AST only |
17+
| test.cpp:477:20:477:24 | test.cpp:478:8:478:12 | AST only |
18+
| test.cpp:484:18:484:23 | test.cpp:485:8:485:12 | AST only |
19+
| test.cpp:490:18:490:22 | test.cpp:491:8:491:12 | AST only |
1520
| true_upon_entry.cpp:9:11:9:16 | true_upon_entry.cpp:13:8:13:8 | IR only |
1621
| true_upon_entry.cpp:62:11:62:16 | true_upon_entry.cpp:66:8:66:8 | IR only |
1722
| true_upon_entry.cpp:98:11:98:16 | true_upon_entry.cpp:105:8:105:8 | IR only |

0 commit comments

Comments
 (0)