Skip to content

Commit 822f6ee

Browse files
committed
Add support for flow through content of global variables
1 parent 197cdab commit 822f6ee

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

go/ql/lib/semmle/go/dataflow/internal/DataFlowPrivate.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ private Field getASparselyUsedChannelTypedField() {
102102
* global or static variable.
103103
*/
104104
predicate jumpStep(Node n1, Node n2) {
105-
exists(ValueEntity v, Write w |
105+
exists(ValueEntity v |
106106
not v instanceof SsaSourceVariable and
107107
not v instanceof Field and
108-
w.writes(v, n1) and
108+
(
109+
any(Write w).writes(v, n1)
110+
or
111+
n1.(DataFlow::PostUpdateNode).getPreUpdateNode() = v.getARead()
112+
) and
109113
n2 = v.getARead()
110114
)
111115
or

go/ql/test/library-tests/semmle/go/dataflow/GlobalVariableSideEffects/globalVariable.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func main() {
1414
test1()
1515
test2()
1616
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)"
17+
sink(globalArray[0]) // $ hasValueFlow="index expression (from source 1)" hasValueFlow="index expression (from source 11)"
18+
sink(globalSlice[0]) // $ hasValueFlow="index expression (from source 2)" hasValueFlow="index expression (from source 12)"
1919
for val := range globalMap1 {
20-
sink(val) // $ MISSING: hasValueFlow="val (from source 3)" hasValueFlow="val (from source 13)"
20+
sink(val) // $ hasValueFlow="val (from source 3)" hasValueFlow="val (from source 13)"
2121
}
2222
for _, val := range globalMap2 {
23-
sink(val) // $ MISSING: hasValueFlow="val (from source 4)" hasValueFlow="val (from source 14)"
23+
sink(val) // $ hasValueFlow="val (from source 4)" hasValueFlow="val (from source 14)"
2424
}
2525
}
2626

@@ -33,29 +33,29 @@ func test1() {
3333
}
3434

3535
func test2() {
36-
taintScalar(&globalScalar, 10)
37-
taintArray(globalArray, 11)
38-
taintSlice(globalSlice, 12)
39-
taintMapKey(globalMap1, 13)
40-
taintMapValue(globalMap2, 14)
36+
taintScalar(&globalScalar)
37+
taintArray(globalArray)
38+
taintSlice(globalSlice)
39+
taintMapKey(globalMap1)
40+
taintMapValue(globalMap2)
4141
}
4242

43-
func taintScalar(x *any, n int) {
44-
*x = source(n)
43+
func taintScalar(x *any) {
44+
*x = source(10)
4545
}
4646

47-
func taintArray(x [1]any, n int) {
48-
x[0] = source(n)
47+
func taintArray(x [1]any) {
48+
x[0] = source(11)
4949
}
5050

51-
func taintSlice(x []any, n int) {
52-
x[0] = source(n)
51+
func taintSlice(x []any) {
52+
x[0] = source(12)
5353
}
5454

55-
func taintMapKey(x map[any]any, n int) {
56-
x[source(n)] = ""
55+
func taintMapKey(x map[any]any) {
56+
x[source(13)] = ""
5757
}
5858

59-
func taintMapValue(x map[any]any, n int) {
60-
x[""] = source(n)
59+
func taintMapValue(x map[any]any) {
60+
x[""] = source(14)
6161
}

0 commit comments

Comments
 (0)