Skip to content

Commit 61593ae

Browse files
authored
Merge pull request #16617 from owen-mc/go/side-effects-on-global-variables
Go: Add tests (mostly failing) for writes to global variables
2 parents 2c4a216 + 7ff1eab commit 61593ae

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import go
2+
import TestUtilities.InlineFlowTest
3+
4+
string getArgString(DataFlow::Node src, DataFlow::Node sink) {
5+
exists(src) and
6+
result =
7+
"\"" + sink.toString() + " (from source " +
8+
src.(DataFlow::CallNode).getArgument(0).getExactValue() + ")\""
9+
}
10+
11+
import ValueFlowTestArgString<DefaultFlowConfig, getArgString/2>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
var globalScalar any
4+
var globalArray [1]any
5+
var globalSlice []any
6+
var globalMap1 map[any]any
7+
var globalMap2 map[any]any
8+
9+
func source(n int) any { return n }
10+
11+
func sink(x any) {}
12+
13+
func main() {
14+
test1()
15+
test2()
16+
sink(globalScalar) // $ hasValueFlow="globalScalar (from source 0)" MISSING: hasValueFlow="globalScalar (from source 10)"
17+
sink(globalArray[0]) // $ MISSING: hasValueFlow="index expression (from source 1)" hasValueFlow="index expression (from source 11)"
18+
sink(globalSlice[0]) // $ MISSING: hasValueFlow="index expression (from source 2)" hasValueFlow="index expression (from source 12)"
19+
for val := range globalMap1 {
20+
sink(val) // $ MISSING: hasValueFlow="val (from source 3)" hasValueFlow="val (from source 13)"
21+
}
22+
for _, val := range globalMap2 {
23+
sink(val) // $ MISSING: hasValueFlow="val (from source 4)" hasValueFlow="val (from source 14)"
24+
}
25+
}
26+
27+
func test1() {
28+
globalScalar = source(0)
29+
globalArray[0] = source(1)
30+
globalSlice[0] = source(2)
31+
globalMap1[source(3)] = nil
32+
globalMap2[""] = source(4)
33+
}
34+
35+
func test2() {
36+
taintScalar(&globalScalar, 10)
37+
taintArray(globalArray, 11)
38+
taintSlice(globalSlice, 12)
39+
taintMapKey(globalMap1, 13)
40+
taintMapValue(globalMap2, 14)
41+
}
42+
43+
func taintScalar(x *any, n int) {
44+
*x = source(n)
45+
}
46+
47+
func taintArray(x [1]any, n int) {
48+
x[0] = source(n)
49+
}
50+
51+
func taintSlice(x []any, n int) {
52+
x[0] = source(n)
53+
}
54+
55+
func taintMapKey(x map[any]any, n int) {
56+
x[source(n)] = ""
57+
}
58+
59+
func taintMapValue(x map[any]any, n int) {
60+
x[""] = source(n)
61+
}

0 commit comments

Comments
 (0)