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

deviceinsight/kafkactl: operations/consumergroups/consumer-group-operation.go; 58 LoC

Open
#18,188 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 [deviceinsight/kafkactl](https://www.github.com/deviceinsight/kafkactl) at [operations/consumergroups/consumer-group-operation.go](https://github.com/deviceinsight/kafkactl/blob/14206aab933a70124a347fa9d2d13ec7b085cc5f/operations/consumergroups/consumer-group-operation.go#L261-L318)

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 topic used in defer or goroutine at line 275

[Click here to see the code in its original context.](https://github.com/deviceinsight/kafkactl/blob/14206aab933a70124a347fa9d2d13ec7b085cc5f/operations/consumergroups/consumer-group-operation.go#L261-L318)

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

```go
for _, topic := range topicsSorted {
if topic != "" {

details := make([]partitionOffset, 0, len(offsets.Blocks[topic]))

var totalLag int64 = 0

partitionChannel := make(chan partitionOffset)
errChannel := make(chan error)

for partition := range offsets.Blocks[topic] {

go func(partition int32) {

newestOffset, err := client.GetOffset(topic, partition, sarama.OffsetNewest)
if err != nil {
errChannel <- errors.Errorf("failed to get newest offset for topic %s partition %d: %v", topic, partition, err)
return
}

oldestOffset, err := client.GetOffset(topic, partition, sarama.OffsetOldest)
if err != nil {
errChannel <- errors.Errorf("failed to get oldest offset for topic %s partition %d: %v", topic, partition, err)
return
}

lead := offsets.Blocks[topic][partition].Offset - oldestOffset
lag := newestOffset - offsets.Blocks[topic][partition].Offset

if !flags.OnlyPartitionsWithLag || lag > 0 {
partitionChannel <- partitionOffset{Partition: partition, NewestOffset: newestOffset, OldestOffset: oldestOffset,
ConsumerOffset: offsets.Blocks[topic][partition].Offset, Lead: lead, Lag: lag}
} else {
partitionChannel <- partitionOffset{Partition: -1}
}
}(partition)
}

for range offsets.Blocks[topic] {
select {
case partitionOffset := <-partitionChannel:
if partitionOffset.Partition != -1 {
details = append(details, partitionOffset)
totalLag += partitionOffset.Lag
}
case err := <-errChannel:
return nil, err
}
}

sort.Slice(details, func(i, j int) bool {
return details[i].Partition < details[j].Partition
})

topicPartitions := topicPartitionOffsets{Name: topic, Partitions: details, TotalLag: totalLag}
topicPartitionList = append(topicPartitionList, topicPartitions)
}
}

```

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: 14206aab933a70124a347fa9d2d13ec7b085cc5f

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.