-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Go: Add tests (mostly failing) for writes to global variables #16617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
owen-mc
merged 1 commit into
github:main
from
owen-mc:go/side-effects-on-global-variables
May 30, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
11 changes: 11 additions & 0 deletions
11
go/ql/test/library-tests/semmle/go/dataflow/GlobalVariableSideEffects/Flows.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import go | ||
| import TestUtilities.InlineFlowTest | ||
|
|
||
| string getArgString(DataFlow::Node src, DataFlow::Node sink) { | ||
| exists(src) and | ||
| result = | ||
| "\"" + sink.toString() + " (from source " + | ||
| src.(DataFlow::CallNode).getArgument(0).getExactValue() + ")\"" | ||
| } | ||
|
|
||
| import ValueFlowTestArgString<DefaultFlowConfig, getArgString/2> | ||
61 changes: 61 additions & 0 deletions
61
go/ql/test/library-tests/semmle/go/dataflow/GlobalVariableSideEffects/globalVariable.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package main | ||
|
|
||
| var globalScalar any | ||
| var globalArray [1]any | ||
| var globalSlice []any | ||
| var globalMap1 map[any]any | ||
| var globalMap2 map[any]any | ||
|
|
||
| func source(n int) any { return n } | ||
|
|
||
| func sink(x any) {} | ||
|
|
||
| func main() { | ||
| test1() | ||
| test2() | ||
| sink(globalScalar) // $ hasValueFlow="globalScalar (from source 0)" MISSING: hasValueFlow="globalScalar (from source 10)" | ||
| sink(globalArray[0]) // $ MISSING: hasValueFlow="index expression (from source 1)" hasValueFlow="index expression (from source 11)" | ||
| sink(globalSlice[0]) // $ MISSING: hasValueFlow="index expression (from source 2)" hasValueFlow="index expression (from source 12)" | ||
| for val := range globalMap1 { | ||
| sink(val) // $ MISSING: hasValueFlow="val (from source 3)" hasValueFlow="val (from source 13)" | ||
| } | ||
| for _, val := range globalMap2 { | ||
| sink(val) // $ MISSING: hasValueFlow="val (from source 4)" hasValueFlow="val (from source 14)" | ||
| } | ||
| } | ||
|
|
||
| func test1() { | ||
| globalScalar = source(0) | ||
| globalArray[0] = source(1) | ||
| globalSlice[0] = source(2) | ||
| globalMap1[source(3)] = nil | ||
| globalMap2[""] = source(4) | ||
| } | ||
|
|
||
| func test2() { | ||
| taintScalar(&globalScalar, 10) | ||
| taintArray(globalArray, 11) | ||
| taintSlice(globalSlice, 12) | ||
| taintMapKey(globalMap1, 13) | ||
| taintMapValue(globalMap2, 14) | ||
| } | ||
|
|
||
| func taintScalar(x *any, n int) { | ||
| *x = source(n) | ||
| } | ||
|
|
||
| func taintArray(x [1]any, n int) { | ||
| x[0] = source(n) | ||
| } | ||
|
|
||
| func taintSlice(x []any, n int) { | ||
| x[0] = source(n) | ||
| } | ||
|
|
||
| func taintMapKey(x map[any]any, n int) { | ||
| x[source(n)] = "" | ||
| } | ||
|
|
||
| func taintMapValue(x map[any]any, n int) { | ||
| x[""] = source(n) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Superfluous 'exists' conjunct.