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

hahwul/mzap: pkg/zap/zap.go; 31 LoC

Open
#15,208 0 comments 0 reactions 0 assignees View on GitHub
fresh small
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [hahwul/mzap](https://www.github.com/hahwul/mzap) at [pkg/zap/zap.go](https://github.com/hahwul/mzap/blob/2496e90dc63313b453890025dd419096eccfe42e/pkg/zap/zap.go#L58-L88)

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 api used in defer or goroutine at line 62

[Click here to see the code in its original context.](https://github.com/hahwul/mzap/blob/2496e90dc63313b453890025dd419096eccfe42e/pkg/zap/zap.go#L58-L88)

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

```go
for _, api := range arrayAPIs {
wg.Add(1)
go func() {
for target := range urlChan {
req, err := http.NewRequest("GET", api+prefix, nil)
if err != nil {
panic(err)
}
q := req.URL.Query()
q.Add("url", target)
req.URL.RawQuery = q.Encode()
if options.APIKey != "" {
req.Header.Add("X-ZAP-API-Key", options.APIKey)
}

client := &http.Client{}
resp, err := client.Do(req)

log.WithFields(log.Fields{
"Target": target,
"ZAP API": api,
}).Info("Added")

if err != nil {
//panic(err)
}
defer resp.Body.Close()
}
wg.Done()
}()
}

```

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: 2496e90dc63313b453890025dd419096eccfe42e

Contributor guide

No contributing guide indexed for this repository

Research direction

Read pkg/zap/zap.go at lines 58-88 and inspect how the range variable api is used inside the goroutine and defer. Compare the analyzer report with the surrounding code, then classify the finding as Bug, Mitigated, or Desirable Behavior using the issue's reaction guidance.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.