Skip to content

Commit 5845528

Browse files
authored
Merge pull request github#12336 from jketema/docs-examples
C++: Add tests for all dataflow examples that occur in our docs
2 parents 484f761 + 3014f20 commit 5845528

23 files changed

+285
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This directory contains the C++ demo queries from the docs directory.
2+
Maintaining this copy should ensure that they continue to work.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| gethostbyname.cpp:6:25:6:44 | https://github.com | gethostbyname.cpp:11:23:11:35 | call to gethostbyname |
2+
| gethostbyname.cpp:9:37:9:56 | https://github.com | gethostbyname.cpp:9:23:9:35 | call to gethostbyname |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import cpp
2+
import semmle.code.cpp.dataflow.new.DataFlow
3+
4+
from StringLiteral sl, FunctionCall fc, DataFlow::Node source, DataFlow::Node sink
5+
where
6+
fc.getTarget().hasName("gethostbyname") and
7+
source.asIndirectExpr(1) = sl and
8+
sink.asIndirectExpr(1) = fc.getArgument(0) and
9+
DataFlow::localFlow(source, sink)
10+
select sl, fc
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| gethostbyname.cpp:6:25:6:44 | https://github.com | gethostbyname.cpp:11:23:11:35 | call to gethostbyname |
2+
| gethostbyname.cpp:9:37:9:56 | https://github.com | gethostbyname.cpp:9:23:9:35 | call to gethostbyname |
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import cpp
2+
import semmle.code.cpp.dataflow.new.DataFlow
3+
4+
class LiteralToGethostbynameConfiguration extends DataFlow::Configuration {
5+
LiteralToGethostbynameConfiguration() { this = "LiteralToGethostbynameConfiguration" }
6+
7+
override predicate isSource(DataFlow::Node source) {
8+
source.asIndirectExpr(1) instanceof StringLiteral
9+
}
10+
11+
override predicate isSink(DataFlow::Node sink) {
12+
exists(FunctionCall fc |
13+
sink.asIndirectExpr(1) = fc.getArgument(0) and
14+
fc.getTarget().hasName("gethostbyname")
15+
)
16+
}
17+
}
18+
19+
from
20+
StringLiteral sl, FunctionCall fc, LiteralToGethostbynameConfiguration cfg, DataFlow::Node source,
21+
DataFlow::Node sink
22+
where
23+
source.asIndirectExpr(1) = sl and
24+
sink.asIndirectExpr(1) = fc.getArgument(0) and
25+
cfg.hasFlow(source, sink)
26+
select sl, fc
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| gethostbyname.cpp:7:30:7:35 | call to getenv | gethostbyname.cpp:13:23:13:35 | call to gethostbyname |
2+
| gethostbyname.cpp:12:37:12:42 | call to getenv | gethostbyname.cpp:12:23:12:35 | call to gethostbyname |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cpp
2+
import semmle.code.cpp.dataflow.new.DataFlow
3+
4+
class GetenvSource extends DataFlow::Node {
5+
GetenvSource() { this.asIndirectExpr(1).(FunctionCall).getTarget().hasQualifiedName("getenv") }
6+
}
7+
8+
class GetenvToGethostbynameConfiguration extends DataFlow::Configuration {
9+
GetenvToGethostbynameConfiguration() { this = "GetenvToGethostbynameConfiguration" }
10+
11+
override predicate isSource(DataFlow::Node source) { source instanceof GetenvSource }
12+
13+
override predicate isSink(DataFlow::Node sink) {
14+
exists(FunctionCall fc |
15+
sink.asIndirectExpr(1) = fc.getArgument(0) and
16+
fc.getTarget().hasName("gethostbyname")
17+
)
18+
}
19+
}
20+
21+
from
22+
Expr getenv, FunctionCall fc, GetenvToGethostbynameConfiguration cfg, DataFlow::Node source,
23+
DataFlow::Node sink
24+
where
25+
source.asIndirectExpr(1) = getenv and
26+
sink.asIndirectExpr(1) = fc.getArgument(0) and
27+
cfg.hasFlow(source, sink)
28+
select getenv, fc
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
| fopen.cpp:6:30:6:37 | a_file |
2+
| fopen.cpp:8:26:8:33 | a_file |
3+
| fopen.cpp:9:26:9:34 | filename1 |
4+
| fopen.cpp:10:26:10:34 | filename2 |
5+
| fopen.cpp:18:18:18:25 | filename |
6+
| fopen.cpp:23:30:23:38 | call to do_getenv |
7+
| fopen.cpp:24:30:24:35 | call to getenv |
8+
| fopen.cpp:27:26:27:31 | call to getenv |
9+
| fopen.cpp:28:26:28:34 | filename1 |
10+
| fopen.cpp:29:26:29:34 | filename2 |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cpp
2+
import semmle.code.cpp.dataflow.new.DataFlow
3+
4+
from Function fopen, FunctionCall fc, Expr src, DataFlow::Node source, DataFlow::Node sink
5+
where
6+
fopen.hasQualifiedName("fopen") and
7+
fc.getTarget() = fopen and
8+
source.asIndirectExpr(1) = src and
9+
sink.asIndirectExpr(1) = fc.getArgument(0) and
10+
DataFlow::localFlow(source, sink)
11+
select src
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| fopen.cpp:18:18:18:25 | filename | This 'fopen' uses data from $@. | fopen.cpp:25:30:25:35 | call to getenv | call to 'getenv' |
2+
| fopen.cpp:18:18:18:25 | filename | This 'fopen' uses data from $@. | fopen.cpp:30:29:30:34 | call to getenv | call to 'getenv' |
3+
| fopen.cpp:27:26:27:31 | call to getenv | This 'fopen' uses data from $@. | fopen.cpp:27:26:27:31 | call to getenv | call to 'getenv' |
4+
| fopen.cpp:28:26:28:34 | filename1 | This 'fopen' uses data from $@. | fopen.cpp:14:12:14:17 | call to getenv | call to 'getenv' |
5+
| fopen.cpp:29:26:29:34 | filename2 | This 'fopen' uses data from $@. | fopen.cpp:24:30:24:35 | call to getenv | call to 'getenv' |

0 commit comments

Comments
 (0)