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

golangchallenge/GCSolutions: june15/normal/matej-kohut/repack.go; 112 LoC

Open
#14,884 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [golangchallenge/GCSolutions](https://www.github.com/golangchallenge/GCSolutions) at [june15/normal/matej-kohut/repack.go](https://github.com/golangchallenge/GCSolutions/blob/61a33d8b44a2c16bc513eccfd309921851cb897a/june15/normal/matej-kohut/repack.go#L76-L187)

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 b used in defer or goroutine at line 113

[Click here to see the code in its original context.](https://github.com/golangchallenge/GCSolutions/blob/61a33d8b44a2c16bc513eccfd309921851cb897a/june15/normal/matej-kohut/repack.go#L76-L187)

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

```go
for _, b := range boxes {
//fmt.Println("Packing box: ", b)
maxScore := float64(0)
maxScoreBox := box{}
maxScorePallet := 0

// channel for all possible box placements with score
boxPlacements := make(chan boxPlacement)
go func() {
// after this function ends (all placments are found) close the channel
defer close(boxPlacements)

cpus := runtime.GOMAXPROCS(0)
wg := sync.WaitGroup{}
// there will be cpus subroutines
wg.Add(cpus)
// index for determining pallet for each subroutine
idx := uint64(0)
palletsLength := len(pallets)

// after function ends and before closing channel, wait for all subroutines to end
defer wg.Wait()

// lanuch subroutines
for n := 0; n < cpus; n++ {
go func() {
// after this subroutine ends, decrement WaitGroup
defer wg.Done()
for {
// use idx to get pallete index, and increase it for other subroutines
pn := int(atomic.AddUint64(&idx, uint64(1))) - 1
// if pallete index is more than palletsLength, end this loop, to end subroutine
if pn >= palletsLength {
break
}
//fmt.Println("Try it on pallet: ", pn)
//fmt.Println("Free space: ", palletsFreeSpace[pn])
if palletsFreeSpace[pn] == 0 || palletsFreeSpace[pn] < uint(b.w*b.l) {
// no free space or too low free space, box can not be placed
continue
}

if palletsFreeSpace[pn] == emptyPalletFreeSpace {
// empty pallet, justify to 0,0 and place horizontal
boxPlacements <- boxPlacement{50.0, box{0, 0, b.w, b.l, b.id}, pn}
} else {
// not empty pallet, geta all posibilities for horizontal orientation
placedBoxes := make(chan box)
pg, _ := pallets[pn].paint()
go getNormalPlacements(b, pg, placedBoxes)
for bp := range placedBoxes {
s := boxScoreOnPallet(bp, pg)
boxPlacements <- boxPlacement{s, bp, pn}
}
}
}
}()
}
}()

for bp := range boxPlacements {
//fmt.Println("Possibility: ", bp)

if bp.s > maxScore {
// get top score
maxScore = bp.s
maxScoreBox = bp.b
maxScorePallet = bp.pn
} else if bp.s == maxScore {
// score is equal, take pallet with less free space
if palletsFreeSpace[maxScorePallet] > palletsFreeSpace[bp.pn] {
maxScoreBox = bp.b
maxScorePallet = bp.pn
} else if palletsFreeSpace[maxScorePallet] == palletsFreeSpace[bp.pn] {
// score is equal, free space is equal, take pallet with smallest index
if maxScorePallet > bp.pn {
maxScoreBox = bp.b
maxScorePallet = bp.pn
} else if maxScorePallet == bp.pn {
// score is equal, free space is equal, pallet index is equal,
// take horizontaly placed box (length > width)
if maxScoreBox.l < bp.b.l {
maxScoreBox = bp.b
} else if maxScoreBox.l == bp.b.l {
// score is equal, free space is equal, pallet index is equal,
// both are horizontaly placed, take the one with lower y placement
if maxScoreBox.y > bp.b.y {
maxScoreBox = bp.b
} else if maxScoreBox.y == bp.b.y {
// score is equal, free space is equal, pallet index is equal,
// both are horizontaly placed on the same y axis, take the lower x placement
if maxScoreBox.x > bp.b.x {
maxScoreBox = bp.b
}
}
}
}
}
}
}
if maxScore > 0 {
//fmt.Println("Winnig pallet: ", maxScorePallet)
// place winning box on winning pallet
pallets[maxScorePallet].boxes = append(pallets[maxScorePallet].boxes, maxScoreBox)
palletsFreeSpace[maxScorePallet] -= uint(maxScoreBox.l * maxScoreBox.w)
} else {
//fmt.Println("No winning pallet, creating new")
// create new pallet and horizonatly orient box on
pallets = append(pallets, pallet{boxes: []box{box{0, 0, b.w, b.l, b.id}}})
palletsFreeSpace = append(palletsFreeSpace, emptyPalletFreeSpace-uint(b.l*b.w))
}
}

```

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: 61a33d8b44a2c16bc513eccfd309921851cb897a

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with june15/normal/matej-kohut/repack.go, especially the range over boxes and the goroutines around lines 76-187. Review how b is used inside deferred or concurrent work, then classify the analyzer finding as Bug, Mitigated, or Desirable Behavior based on the actual behavior and leave the corresponding reaction.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.