Skip to content

Commit 2a1ba6d

Browse files
committed
C++: Share configurations in testcases
1 parent b9af112 commit 2a1ba6d

File tree

6 files changed

+74
-130
lines changed

6 files changed

+74
-130
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
private import semmle.code.cpp.dataflow.DataFlow
2+
private import DataFlow
3+
4+
class Conf extends Configuration {
5+
Conf() { this = "FieldFlowConf" }
6+
7+
override predicate isSource(Node src) {
8+
src.asExpr() instanceof NewExpr
9+
or
10+
src.asExpr().(Call).getTarget().hasName("user_input")
11+
or
12+
exists(FunctionCall fc |
13+
fc.getAnArgument() = src.asDefiningArgument() and
14+
fc.getTarget().hasName("argument_source")
15+
)
16+
}
17+
18+
override predicate isSink(Node sink) {
19+
exists(Call c |
20+
c.getTarget().hasName("sink") and
21+
c.getAnArgument() = sink.asExpr()
22+
)
23+
}
24+
25+
override predicate isAdditionalFlowStep(Node a, Node b) {
26+
b.asPartialDefinition() =
27+
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
28+
.getQualifier()
29+
or
30+
b.asExpr().(AddressOfExpr).getOperand() = a.asExpr()
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
private import semmle.code.cpp.ir.dataflow.DataFlow
2+
private import DataFlow
3+
4+
class Conf extends Configuration {
5+
Conf() { this = "FieldFlowConf" }
6+
7+
override predicate isSource(Node src) {
8+
src.asExpr() instanceof NewExpr
9+
or
10+
src.asExpr().(Call).getTarget().hasName("user_input")
11+
or
12+
exists(FunctionCall fc |
13+
fc.getAnArgument() = src.asDefiningArgument() and
14+
fc.getTarget().hasName("argument_source")
15+
)
16+
}
17+
18+
override predicate isSink(Node sink) {
19+
exists(Call c |
20+
c.getTarget().hasName("sink") and
21+
c.getAnArgument() = sink.asExpr()
22+
)
23+
}
24+
25+
override predicate isAdditionalFlowStep(Node a, Node b) {
26+
b.asPartialDefinition() =
27+
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
28+
.getQualifier()
29+
or
30+
b.asExpr().(AddressOfExpr).getOperand() = a.asExpr()
31+
}
32+
}

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,19 @@
44

55
import TestUtilities.InlineExpectationsTest
66
import semmle.code.cpp.dataflow.DataFlow
7-
import DataFlow
7+
import ASTConfiguration
88
import cpp
99

10-
class Conf extends Configuration {
11-
Conf() { this = "FieldFlowConf" }
12-
13-
override predicate isSource(Node src) {
14-
src.asExpr() instanceof NewExpr
15-
or
16-
src.asExpr().(Call).getTarget().hasName("user_input")
17-
or
18-
exists(FunctionCall fc |
19-
fc.getAnArgument() = src.asDefiningArgument() and
20-
fc.getTarget().hasName("argument_source")
21-
)
22-
}
23-
24-
override predicate isSink(Node sink) {
25-
exists(Call c |
26-
c.getTarget().hasName("sink") and
27-
c.getAnArgument() = sink.asExpr()
28-
)
29-
}
30-
31-
override predicate isAdditionalFlowStep(Node a, Node b) {
32-
b.asPartialDefinition() =
33-
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
34-
.getQualifier()
35-
or
36-
b.asExpr().(AddressOfExpr).getOperand() = a.asExpr()
37-
}
38-
}
39-
4010
class ASTFieldFlowTest extends InlineExpectationsTest {
4111
ASTFieldFlowTest() { this = "ASTFieldFlowTest" }
4212

4313
override string getARelevantTag() { result = "ast" }
4414

4515
override predicate hasActualResult(Location location, string element, string tag, string value) {
46-
exists(Node source, Node sink, Conf conf, int n |
16+
exists(DataFlow::Node source, DataFlow::Node sink, Conf conf, int n |
4717
tag = "ast" and
4818
conf.hasFlow(source, sink) and
49-
n = strictcount(Node otherSource | conf.hasFlow(otherSource, sink)) and
19+
n = strictcount(DataFlow::Node otherSource | conf.hasFlow(otherSource, sink)) and
5020
(
5121
n = 1 and value = ""
5222
or

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,19 @@
44

55
import TestUtilities.InlineExpectationsTest
66
import semmle.code.cpp.ir.dataflow.DataFlow
7-
import DataFlow
7+
import IRConfiguration
88
import cpp
99

10-
class Conf extends Configuration {
11-
Conf() { this = "FieldFlowConf" }
12-
13-
override predicate isSource(Node src) {
14-
src.asExpr() instanceof NewExpr
15-
or
16-
src.asExpr().(Call).getTarget().hasName("user_input")
17-
or
18-
exists(FunctionCall fc |
19-
fc.getAnArgument() = src.asDefiningArgument() and
20-
fc.getTarget().hasName("argument_source")
21-
)
22-
}
23-
24-
override predicate isSink(Node sink) {
25-
exists(Call c |
26-
c.getTarget().hasName("sink") and
27-
c.getAnArgument() = sink.asExpr()
28-
)
29-
}
30-
31-
override predicate isAdditionalFlowStep(Node a, Node b) {
32-
b.asPartialDefinition() =
33-
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
34-
.getQualifier()
35-
or
36-
b.asExpr().(AddressOfExpr).getOperand() = a.asExpr()
37-
}
38-
}
39-
4010
class IRFieldFlowTest extends InlineExpectationsTest {
4111
IRFieldFlowTest() { this = "IRFieldFlowTest" }
4212

4313
override string getARelevantTag() { result = "ir" }
4414

4515
override predicate hasActualResult(Location location, string element, string tag, string value) {
46-
exists(Node source, Node sink, Conf conf, int n |
16+
exists(DataFlow::Node source, DataFlow::Node sink, Conf conf, int n |
4717
tag = "ir" and
4818
conf.hasFlow(source, sink) and
49-
n = strictcount(Node otherSource | conf.hasFlow(otherSource, sink)) and
19+
n = strictcount(DataFlow::Node otherSource | conf.hasFlow(otherSource, sink)) and
5020
(
5121
n = 1 and value = ""
5222
or

cpp/ql/test/library-tests/dataflow/fields/ir-path-flow.ql

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,9 @@
33
*/
44

55
import semmle.code.cpp.ir.dataflow.DataFlow
6-
import DataFlow
6+
import IRConfiguration
77
import cpp
8-
import PathGraph
9-
10-
class Conf extends Configuration {
11-
Conf() { this = "FieldFlowConf" }
12-
13-
override predicate isSource(Node src) {
14-
src.asExpr() instanceof NewExpr
15-
or
16-
src.asExpr().(Call).getTarget().hasName("user_input")
17-
or
18-
exists(FunctionCall fc |
19-
fc.getAnArgument() = src.asDefiningArgument() and
20-
fc.getTarget().hasName("argument_source")
21-
)
22-
}
23-
24-
override predicate isSink(Node sink) {
25-
exists(Call c |
26-
c.getTarget().hasName("sink") and
27-
c.getAnArgument() = sink.asExpr()
28-
)
29-
}
30-
31-
override predicate isAdditionalFlowStep(Node a, Node b) {
32-
b.asPartialDefinition() =
33-
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
34-
.getQualifier()
35-
or
36-
b.asExpr().(AddressOfExpr).getOperand() = a.asExpr()
37-
}
38-
}
8+
import DataFlow::PathGraph
399

4010
from DataFlow::PathNode src, DataFlow::PathNode sink, Conf conf
4111
where conf.hasFlowPath(src, sink)

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

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,9 @@
33
*/
44

55
import semmle.code.cpp.dataflow.DataFlow
6-
import DataFlow
6+
import ASTConfiguration
77
import cpp
8-
import PathGraph
9-
10-
class Conf extends Configuration {
11-
Conf() { this = "FieldFlowConf" }
12-
13-
override predicate isSource(Node src) {
14-
src.asExpr() instanceof NewExpr
15-
or
16-
src.asExpr().(Call).getTarget().hasName("user_input")
17-
or
18-
exists(FunctionCall fc |
19-
fc.getAnArgument() = src.asDefiningArgument() and
20-
fc.getTarget().hasName("argument_source")
21-
)
22-
}
23-
24-
override predicate isSink(Node sink) {
25-
exists(Call c |
26-
c.getTarget().hasName("sink") and
27-
c.getAnArgument() = sink.asExpr()
28-
)
29-
}
30-
31-
override predicate isAdditionalFlowStep(Node a, Node b) {
32-
b.asPartialDefinition() =
33-
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
34-
.getQualifier()
35-
or
36-
b.asExpr().(AddressOfExpr).getOperand() = a.asExpr()
37-
}
38-
}
8+
import DataFlow::PathGraph
399

4010
from DataFlow::PathNode src, DataFlow::PathNode sink, Conf conf
4111
where conf.hasFlowPath(src, sink)

0 commit comments

Comments
 (0)