Skip to content

Commit 5af3e11

Browse files
committed
Test value flow through SliceExpr with array content
1 parent 803ed20 commit 5af3e11

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

go/ql/test/library-tests/semmle/go/dataflow/ArrayConversion/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ func source() string {
44
return "untrusted data"
55
}
66

7-
func sink(string) {
7+
func sink(any) {
88
}
99

1010
func sliceToArray(p []string) [1]string {
@@ -15,11 +15,15 @@ func main() {
1515
// Test the new slice->array conversion permitted in Go 1.20
1616
var a [4]string
1717
a[0] = source()
18-
alias := sliceToArray(a[:])
18+
alias := [2]string(a[:])
1919
sink(alias[0]) // $ hasTaintFlow="index expression"
20+
sink(alias[1]) // $ SPURIOUS: hasTaintFlow="index expression" // we don't distinguish different elements of arrays or slices
21+
sink(alias) // $ hasTaintFlow="alias"
2022

2123
// Compare with the standard dataflow support for arrays
2224
var b [4]string
2325
b[0] = source()
2426
sink(b[0]) // $ hasValueFlow="index expression"
27+
sink(b[1]) // $ SPURIOUS: hasValueFlow="index expression" // we don't distinguish different elements of arrays or slices
28+
sink(b) // $ hasTaintFlow="b"
2529
}

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

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import go
2+
import TestUtilities.InlineFlowTest
3+
import DefaultFlowTest
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
func source() string {
4+
return "untrusted data"
5+
}
6+
7+
func sink(any) {
8+
}
9+
10+
func main() {
11+
}
12+
13+
// Value flow with array content through slice expressions
14+
15+
func arrayBase(base [4]string) {
16+
base[1] = source()
17+
slice := base[1:4]
18+
sink(slice[0]) // $ hasTaintFlow="index expression"
19+
sink(slice[1]) // $ SPURIOUS: hasTaintFlow="index expression" // we don't distinguish different elements of arrays or slices
20+
sink(slice) // $ hasTaintFlow="slice"
21+
}
22+
23+
func arrayPointerBase(base *[4]string) {
24+
base[1] = source()
25+
slice := base[1:4]
26+
sink(slice[0]) // $ hasTaintFlow="index expression"
27+
sink(slice[1]) // $ SPURIOUS: hasTaintFlow="index expression" // we don't distinguish different elements of arrays or slices
28+
sink(slice) // $ hasTaintFlow="slice"
29+
}
30+
31+
func sliceBase(base []string) {
32+
base[1] = source()
33+
slice := base[1:4]
34+
sink(slice[0]) // $ hasTaintFlow="index expression"
35+
sink(slice[1]) // $ SPURIOUS: hasTaintFlow="index expression" // we don't distinguish different elements of arrays or slices
36+
sink(slice) // $ hasTaintFlow="slice"
37+
}
38+
39+
func slicePointerBase(base *[]string) {
40+
(*base)[1] = source()
41+
slice := (*base)[1:4]
42+
sink(slice[0]) // $ hasTaintFlow="index expression"
43+
sink(slice[1]) // $ SPURIOUS: hasTaintFlow="index expression" // we don't distinguish different elements of arrays or slices
44+
sink(slice) // $ hasTaintFlow="slice"
45+
}

0 commit comments

Comments
 (0)