Skip to content

Commit 281c944

Browse files
authored
Merge pull request #774 from jbj/dataflow-asExpr-conversion
Approved by dave-bartolomeo
2 parents e6672aa + 22b1503 commit 281c944

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,22 @@ class Node extends Instruction {
2424
result = this.getResultType()
2525
}
2626

27-
/** Gets the expression corresponding to this node, if any. */
28-
Expr asExpr() { result = this.getConvertedResultExpression() }
27+
/**
28+
* Gets the non-conversion expression corresponding to this node, if any. If
29+
* this node strictly (in the sense of `asConvertedExpr`) corresponds to a
30+
* `Conversion`, then the result is that `Conversion`'s non-`Conversion` base
31+
* expression.
32+
*/
33+
Expr asExpr() {
34+
result.getConversion*() = this.getConvertedResultExpression() and
35+
not result instanceof Conversion
36+
}
37+
38+
/**
39+
* Gets the expression corresponding to this node, if any. The returned
40+
* expression may be a `Conversion`.
41+
*/
42+
Expr asConvertedExpr() { result = this.getConvertedResultExpression() }
2943

3044
/** Gets the parameter corresponding to this node, if any. */
3145
Parameter asParameter() { result = this.(InitializeParameterInstruction).getParameter() }
@@ -48,10 +62,21 @@ class Node extends Instruction {
4862
* An expression, viewed as a node in a data flow graph.
4963
*/
5064
class ExprNode extends Node {
51-
Expr expr;
65+
ExprNode() { exists(this.asExpr()) }
66+
67+
/**
68+
* Gets the non-conversion expression corresponding to this node, if any. If
69+
* this node strictly (in the sense of `getConvertedExpr`) corresponds to a
70+
* `Conversion`, then the result is that `Conversion`'s non-`Conversion` base
71+
* expression.
72+
*/
73+
Expr getExpr() { result = this.asExpr() }
5274

53-
ExprNode() { expr = this.asExpr() }
54-
Expr getExpr() { result = expr }
75+
/**
76+
* Gets the expression corresponding to this node, if any. The returned
77+
* expression may be a `Conversion`.
78+
*/
79+
Expr getConvertedExpr() { result = this.asConvertedExpr() }
5580
}
5681

5782
/**
@@ -98,10 +123,17 @@ abstract class PostUpdateNode extends Node {
98123
}
99124

100125
/**
101-
* Gets the `Node` corresponding to `e`.
126+
* Gets a `Node` corresponding to `e` or any of its conversions. There is no
127+
* result if `e` is a `Conversion`.
102128
*/
103129
ExprNode exprNode(Expr e) { result.getExpr() = e }
104130

131+
/**
132+
* Gets the `Node` corresponding to `e`, if any. Here, `e` may be a
133+
* `Conversion`.
134+
*/
135+
ExprNode convertedExprNode(Expr e) { result.getExpr() = e }
136+
105137
/**
106138
* Gets the `Node` corresponding to the value of `p` at function entry.
107139
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestAllocationConfig extends DataFlow::Configuration {
1919
override predicate isSink(DataFlow::Node sink) {
2020
exists(FunctionCall call |
2121
call.getTarget().getName() = "sink" and
22-
sink.asExpr() = call.getAnArgument().getFullyConverted()
22+
sink.asExpr() = call.getAnArgument()
2323
)
2424
}
2525

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
int source();
2-
void sink(...);
2+
void sink(int); void sink(const int *); void sink(int **);
33

44
void intraprocedural_with_local_flow() {
55
int t2;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
| test.cpp:66:30:66:36 | test.cpp:71:8:71:9 | AST only |
2-
| test.cpp:89:28:89:34 | test.cpp:90:8:90:14 | AST only |
2+
| test.cpp:89:28:89:34 | test.cpp:92:8:92:14 | IR only |
33
| test.cpp:100:13:100:18 | test.cpp:103:10:103:12 | AST only |
44
| test.cpp:120:9:120:20 | test.cpp:126:8:126:19 | AST only |
55
| test.cpp:122:18:122:30 | test.cpp:132:22:132:23 | IR only |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
| test.cpp:76:8:76:9 | Load: u1 | test.cpp:75:7:75:8 | Uninitialized: definition of u1 |
1010
| test.cpp:84:8:84:18 | Load: ... ? ... : ... | test.cpp:83:7:83:8 | Uninitialized: definition of u2 |
1111
| test.cpp:86:8:86:9 | Load: i1 | test.cpp:83:7:83:8 | Uninitialized: definition of u2 |
12+
| test.cpp:90:8:90:14 | Load: source1 | test.cpp:89:28:89:34 | InitializeParameter: source1 |
13+
| test.cpp:92:8:92:14 | Load: source1 | test.cpp:89:28:89:34 | InitializeParameter: source1 |
1214
| test.cpp:132:22:132:23 | Load: m1 | test.cpp:122:18:122:30 | InitializeParameter: sourceStruct1 |
1315
| test.cpp:140:22:140:23 | Load: m1 | test.cpp:122:18:122:30 | InitializeParameter: sourceStruct1 |
1416
| test.cpp:188:8:188:8 | Load: y | test.cpp:186:27:186:32 | Call: call to source |

0 commit comments

Comments
 (0)