github-vet / github-vet/rangeloop-pointer-findings

emicklei/go-selfdiagnose: registry.go; 37 LoC

Open
#11,578 0 comments 0 reactions 0 assignees View on GitHub
fresh small
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [emicklei/go-selfdiagnose](https://www.github.com/emicklei/go-selfdiagnose) at [registry.go](https://github.com/emicklei/go-selfdiagnose/blob/ad715b8a18569a83f73cf79873ebde6ff56924ce/registry.go#L29-L65)

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 each used in defer or goroutine at line 35

[Click here to see the code in its original context.](https://github.com/emicklei/go-selfdiagnose/blob/ad715b8a18569a83f73cf79873ebde6ff56924ce/registry.go#L29-L65)

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

```go
for _, each := range r.tasks {
resultCh := make(chan *Result, 1)
now := time.Now()
go func() {
defer close(resultCh)
res := new(Result)
if t, ok := each.(HasSeverity); ok {
res.Severity = t.Severity()
}
res.Target = each
each.Run(ctx, res)
resultCh <- res
}()
timeout := 1 * time.Second
// task can override the non-zero value
if t, ok := each.(HasTimeout); ok {
if t.Timeout() > 0 {
timeout = t.Timeout()
}
}
var result *Result
select {
case <-time.After(timeout):
// task took longer than timeout so create result to report that
res := new(Result)
if t, ok := each.(HasSeverity); ok {
res.Severity = t.Severity()
}
res.Target = each
res.Passed = false
res.Reason = "task did not complete within timeout"
result = res
case result, _ = <-resultCh:
}
result.CompletedIn = time.Now().Sub(now)
results = append(results, result)
}

```

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: ad715b8a18569a83f73cf79873ebde6ff56924ce

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with registry.go lines 29-65 and review the analyzer warning about the range-loop variable used in the goroutine and defer. Determine whether the reported behavior is a Bug, Mitigated, or Desirable Behavior, then leave the corresponding reaction on the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
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.