Skip to content

Commit 368ca6c

Browse files
committed
Add test exercising Go 1.20 array conversions
1 parent 2cd1e09 commit 368ca6c

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

go/ql/test/library-tests/semmle/go/dataflow/ArrayConversion/Flows.expected

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import go
2+
import TestUtilities.InlineExpectationsTest
3+
4+
class DataConfiguration extends DataFlow::Configuration {
5+
DataConfiguration() { this = "data-configuration" }
6+
7+
override predicate isSource(DataFlow::Node source) {
8+
source = any(DataFlow::CallNode c | c.getCalleeName() = "source").getResult(0)
9+
}
10+
11+
override predicate isSink(DataFlow::Node sink) {
12+
sink = any(DataFlow::CallNode c | c.getCalleeName() = "sink").getArgument(0)
13+
}
14+
}
15+
16+
class DataFlowTest extends InlineExpectationsTest {
17+
DataFlowTest() { this = "DataFlowTest" }
18+
19+
override string getARelevantTag() { result = "dataflow" }
20+
21+
override predicate hasActualResult(Location location, string element, string tag, string value) {
22+
tag = "dataflow" and
23+
exists(DataFlow::Node sink | any(DataConfiguration c).hasFlow(_, sink) |
24+
element = sink.toString() and
25+
value = "" and
26+
sink.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
27+
location.getStartColumn(), location.getEndLine(), location.getEndColumn())
28+
)
29+
}
30+
}
31+
32+
class TaintConfiguration extends TaintTracking::Configuration {
33+
TaintConfiguration() { this = "taint-configuration" }
34+
35+
override predicate isSource(DataFlow::Node source) {
36+
source = any(DataFlow::CallNode c | c.getCalleeName() = "source").getResult(0)
37+
}
38+
39+
override predicate isSink(DataFlow::Node sink) {
40+
sink = any(DataFlow::CallNode c | c.getCalleeName() = "sink").getArgument(0)
41+
}
42+
}
43+
44+
class TaintFlowTest extends InlineExpectationsTest {
45+
TaintFlowTest() { this = "TaintFlowTest" }
46+
47+
override string getARelevantTag() { result = "taintflow" }
48+
49+
override predicate hasActualResult(Location location, string element, string tag, string value) {
50+
tag = "taintflow" and
51+
exists(DataFlow::Node sink | any(TaintConfiguration c).hasFlow(_, sink) |
52+
element = sink.toString() and
53+
value = "" and
54+
sink.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
55+
location.getStartColumn(), location.getEndLine(), location.getEndColumn())
56+
)
57+
}
58+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
func source() string {
4+
return "untrusted data"
5+
}
6+
7+
func sink(string) {
8+
}
9+
10+
func sliceToArray(p []string) [1]string {
11+
return [1]string(p)
12+
}
13+
14+
func main() {
15+
// Test the new slice->array conversion permitted in Go 1.20
16+
var a [4]string
17+
a[0] = source()
18+
alias := sliceToArray(a[:])
19+
sink(alias[0]) // $ taintflow
20+
21+
// Compare with the standard dataflow support for arrays
22+
var b [4]string
23+
b[0] = source()
24+
sink(b[0]) // $ taintflow
25+
}
26+

0 commit comments

Comments
 (0)