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

hscells/groove: rank/runner.go; 52 LoC

Open
#7,666 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [hscells/groove](https://www.github.com/hscells/groove) at [rank/runner.go](https://github.com/hscells/groove/blob/8c20ac6a00a1c672d22742c32e84ec993b088fb3/rank/runner.go#L174-L225)

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 query used in defer or goroutine at line 194

[Click here to see the code in its original context.](https://github.com/hscells/groove/blob/8c20ac6a00a1c672d22742c32e84ec993b088fb3/rank/runner.go#L174-L225)

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

```go
for _, query := range r.queries {
posting, err := newPosting(query, indexPath, r.EntrezStatisticsSource)
if err != nil {
errc <- err
return
}
r.scorer.posting(posting)
r.scorer.entrez(r.EntrezStatisticsSource)

fmt.Println("scoring documents")
scored := make([]ScoredDocument, len(posting.DocLens))
i := 0
bar := pb.New(len(posting.DocLens))
bar.Start()
sem := make(chan bool, runtime.NumCPU())
var mu sync.Mutex
for pmid := range posting.DocLens {
sem <- true
go func(p string) {
defer func() { <-sem }()
score, err := r.scorer.Score(query, p, r.fields...)
if err != nil {
errc <- err
return
}
mu.Lock()
defer mu.Unlock()
scored[i] = ScoredDocument{
PMID: p,
Score: score,
}
i++
bar.Increment()
return
}(pmid)
}
// Wait until the last goroutine has read from the semaphore.
for i := 0; i < cap(sem); i++ {
sem <- true
}
bar.Finish()
fmt.Println("sorting scored docs")
sort.Slice(scored, func(i, j int) bool {
return scored[i].Score > scored[j].Score
})
for i := range scored {
scored[i].Rank = float64(i + 1)
}
scoredc <- ScoredDocuments{
Docs: scored,
}
}

```

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: 8c20ac6a00a1c672d22742c32e84ec993b088fb3

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.