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

Azure/helm-elasticstack: tools/elasticwatcher/main.go; 54 LoC

Open
#13,011 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 [Azure/helm-elasticstack](https://www.github.com/Azure/helm-elasticstack) at [tools/elasticwatcher/main.go](https://github.com/Azure/helm-elasticstack/blob/0976164cb8ad4e871b20b56072b04312f6416fd6/tools/elasticwatcher/main.go#L478-L531)

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 watch used in defer or goroutine at line 487

[Click here to see the code in its original context.](https://github.com/Azure/helm-elasticstack/blob/0976164cb8ad4e871b20b56072b04312f6416fd6/tools/elasticwatcher/main.go#L478-L531)

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

```go
for _, watch := range cfg.Watches {
reader, writer := io.Pipe()
wg := sync.WaitGroup{}
wg.Add(2)
errc := make(chan error, 1)
go func() {
defer wg.Done()
defer writer.Close()
enc := json.NewEncoder(writer)
enc.Encode(watch.Body) // #nosec
}()

go func() {
defer wg.Done()
defer reader.Close()
watcherURL := buildWatcherURL(c.host, c.port, watch.Name)
req, err := http.NewRequest(http.MethodPut, watcherURL, reader)
if err != nil {
errc <- fmt.Errorf("Failed to build the request to create the watch: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
err = setBasicAuth(req, c.authFile)
if err != nil {
errc <- fmt.Errorf("Failed to set Basic Auth header: %v", err)
return
}

client := buildHTTPClient()
resp, err := client.Do(req)
if err != nil {
errc <- fmt.Errorf("Failed to execute the watch create/update request: %v", err)
return
}
defer resp.Body.Close()

if resp.StatusCode >= http.StatusBadRequest {
content, _ := ioutil.ReadAll(resp.Body) // #nosec
errc <- fmt.Errorf(
"Failed to create/update the watch:\n Status Code: %d.\n Error Message: %s",
resp.StatusCode, string(content))
return
}
errc <- nil
}()

wg.Wait()

if err := <-errc; err != nil {
fmt.Println(err)
return subcommands.ExitFailure
}
fmt.Printf("Successfully created/updated the watch '%s'.\n", watch.Name)
}

```

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: 0976164cb8ad4e871b20b56072b04312f6416fd6

Contributor guide

No contributing guide indexed for this repository

Research direction

Read tools/elasticwatcher/main.go around lines 478-531, starting with the range loop and the two goroutines that use watch. Determine whether the reported range-loop capture affects the watch body or name, then verify the behavior with the project's available checks; done means the analyzer finding is addressed or explicitly classified as mitigated or desirable.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.