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

oskanberg/olson-model: main.go; 43 LoC

Open
#7,659 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 [oskanberg/olson-model](https://www.github.com/oskanberg/olson-model) at [main.go](https://github.com/oskanberg/olson-model/blob/b2c740b234e9defc8d47036f53d0c2bf823573c8/main.go#L87-L129)

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 prd used in defer or goroutine at line 123

[Click here to see the code in its original context.](https://github.com/oskanberg/olson-model/blob/b2c740b234e9defc8d47036f53d0c2bf823573c8/main.go#L87-L129)

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

```go
for _, prd := range s.predators {
predatorGenome = prd.GetGenome()
wg.Add(1)
go func() {
simulation := NewSimulation()
for i := 0; i < NumberOfPredatorClones; i++ {
simulation.AddPredatorFromGenome(predatorGenome)
}

for _, pry := range s.prey {
preyGenome = pry.GetGenome()
for i := 0; i < NumberOfPreyClones; i++ {
simulation.AddPreyFromGenome(preyGenome)
}

for i := 0; i < RoundsPerGeneration; i++ {
simulation.Simulate(TotalSimulationSteps)
simulation.ResetPopulation()
}

// copy prey fitness back
var newFitness int
for _, sPry := range simulation.GetPrey() {
newFitness += sPry.GetFitness()
}
newFitness = newFitness / NumberOfPreyClones
// fmt.Println("prey updating with fitness", newFitness)
pry.fitness = newFitness

newFitness = 0
for _, sPrd := range simulation.GetPredators() {
newFitness += sPrd.GetFitness()
}
newFitness = int(math.Ceil(float64(newFitness) / float64(NumberOfPredatorClones)))
// fmt.Println("predator updating with fitness", newFitness)
// += to other prey adversaries
prd.fitness += newFitness
meanNearbyPrey += simulation.meanNearbyPrey
}
// prd.fitness /= len(s.prey)
wg.Done()
}()
}

```

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

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.