github-vet / github-vet/rangeloop-pointer-findings
Azure/helm-elasticstack: tools/elastictemplate/main.go; 54 LoC
- 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/elastictemplate/main.go](https://github.com/Azure/helm-elasticstack/blob/0976164cb8ad4e871b20b56072b04312f6416fd6/tools/elastictemplate/main.go#L346-L399)
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 template used in defer or goroutine at line 355
[Click here to see the code in its original context.](https://github.com/Azure/helm-elasticstack/blob/0976164cb8ad4e871b20b56072b04312f6416fd6/tools/elastictemplate/main.go#L346-L399)
Click here to show the 54 line(s) of Go which triggered the analyzer.
```go
for _, template := range cfg.Templates {
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(template.Body) // #nosec
}()
go func() {
defer wg.Done()
defer reader.Close()
templateURL := buildTemplateURL(c.host, c.port, template.Name)
req, err := http.NewRequest(http.MethodPut, templateURL, reader)
if err != nil {
errc <- fmt.Errorf("Failed to build the request to create the template: %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 template 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 template:\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 template '%s'.\n", template.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
Assessment
This issue has not been assessed yet.