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

profefe/kube-profefe: pkg/cmd/capture.go; 64 LoC

Open
#7,316 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 [profefe/kube-profefe](https://www.github.com/profefe/kube-profefe) at [pkg/cmd/capture.go](https://github.com/profefe/kube-profefe/blob/2d933a0a63ba314e5da69c54951032a09ad472bc/pkg/cmd/capture.go#L95-L158)

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 target used in defer or goroutine at line 116

[Click here to see the code in its original context.](https://github.com/profefe/kube-profefe/blob/2d933a0a63ba314e5da69c54951032a09ad472bc/pkg/cmd/capture.go#L95-L158)

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

```go
for _, target := range selectedPods {
stopCh := make(chan struct{}, 1)
readyCh := make(chan struct{})
var berr, bout bytes.Buffer
buffErr := bufio.NewWriter(&berr)
buffOut := bufio.NewWriter(&bout)

// TODO: make it smater
localPort := 9000
for ; localPort < 9009; localPort++ {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", localPort))
if err != nil {
continue
}
ln.Close()
break
}

go func() {
err = kubeutil.PortForwardAPod(kubeutil.PortForwardAPodRequest{
RestConfig: config,
Pod: target,
LocalPort: localPort,
Streams: genericclioptions.IOStreams{
In: os.Stdin,
Out: buffOut,
ErrOut: buffErr,
},
StopCh: stopCh,
ReadyCh: readyCh,
})
if err != nil {
panic(err)
}
}()

select {
case <-readyCh:
break
}

println("gathering profiles for pod: " + target.Name)

profiles, err := pprofutil.GatherAllByPod(context.Background(), logger, DefaultForwardHost, target, localPort)
if err != nil {
panic(err)
}

var pClient *profefe.Client
if ProfefeHostPortE != "" {
pClient = profefe.NewClient(profefe.Config{
HostPort: ProfefeHostPortE,
}, http.Client{})
}

err = writeProfiles(context.Background(), pClient, profiles, target)
if err != nil {
panic(err)
}

close(stopCh)
buffErr.Flush()
buffOut.Flush()
}

```

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: 2d933a0a63ba314e5da69c54951032a09ad472bc

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.