Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ff1eab

Browse files
committed
Add tests (mostly failing) for writes to global variables
This was based on the equivalent for java: #16500
1 parent 7756259 commit 7ff1eab

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

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

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
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>
Lines changed: 61 additions & 0 deletions
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)