github-vet / github-vet/preliminary-findings-jan-2021

terra-project/mantle-sdk: graph/local_run.go; 59 LoC

Open
#1,908 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Found a possible issue in [terra-project/mantle-sdk](https://www.github.com/terra-project/mantle-sdk) at [graph/local_run.go](https://github.com/terra-project/mantle-sdk/blob/c4df7e5a2bc83b9712042bed95635caca461a3b6/graph/local_run.go#L57-L115)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable selection used in defer or goroutine at line 61

[Click here to see the code in its original context.](https://github.com/terra-project/mantle-sdk/blob/c4df7e5a2bc83b9712042bed95635caca461a3b6/graph/local_run.go#L57-L115)

Click here to show the 59 line(s) of Go which triggered the analyzer.

```go
for _, selection := range querySelections {
go func() {
defer wg.Done()

switch selection := selection.(type) {
case *ast.Field:
fieldName := selection.Name
fieldConfig := fields[fieldName.Value]

var variables = make(map[string]interface{})

// map arguments to query arguments
if len(selection.Arguments) > 0 {
for _, argument := range selection.Arguments {
argumentName := argument.Name.Value
switch argumentValue := argument.Value.GetValue().(type) {
case *ast.Name:
variables[argumentName] = p.VariableValues[argumentValue.Value]
case ast.Variable:
variables[argumentName] = p.VariableValues[argumentValue.Name.Value]
default:
variables[argumentName] = argumentValue
}
}
}

rp := graphql.ResolveParams{
Source: source,
Args: variables,
Info: graphql.ResolveInfo{},
Context: p.Context,
}

// mutex control
sync.Lock()
defer sync.Unlock()

result, err := unthunkResult(fieldConfig.Resolve(rp))

if err != nil {
gqlRunResult.Errors = []error{err}
return
}

pack, packErr := serdes.Serialize(nil, result)
if packErr != nil {
gqlRunResult.Errors = []error{err}
return
}

// save
gqlRunResult.Data[fieldName.Value] = pack
return

case *ast.FragmentDefinition:
// noop yet
}
}()
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: c4df7e5a2bc83b9712042bed95635caca461a3b6

Contributor guide

No contributing guide indexed for this repository

Research direction

Read graph/local_run.go around lines 57-115 and inspect the range loop together with the goroutine and defer. Confirm the analyzer warning against the referenced commit, then make the smallest correction that preserves the intended selection for each goroutine and verify the warning is gone.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, graphql
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.