github-vet / github-vet/rangeloop-pointer-findings
ejdoh1/pialive: main.go; 43 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [ejdoh1/pialive](https://www.github.com/ejdoh1/pialive) at [main.go](https://github.com/ejdoh1/pialive/blob/4679767cdd4d6fe3b9d1a93a07a573449f90c295/main.go#L55-L97)
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 broker used in defer or goroutine at line 58
[Click here to see the code in its original context.](https://github.com/ejdoh1/pialive/blob/4679767cdd4d6fe3b9d1a93a07a573449f90c295/main.go#L55-L97)
Click here to show the 43 line(s) of Go which triggered the analyzer.
```go
for _, broker := range brokers {
log.Println("Starting connection for broker:", broker)
go func() {
opts := mqtt.NewClientOptions().AddBroker(broker).SetClientID(clientID).SetAutoReconnect(true)
c := mqtt.NewClient(opts)
for {
if token := c.Connect(); token.Wait() && token.Error() != nil {
log.Println("Failed to connect to broker:", broker)
time.Sleep(time.Duration(cfg.ReconnectIntervalSec) * time.Second)
} else {
log.Println("Connected to broker:", broker)
break
}
}
for {
m, e := runCmd(cfg.Command)
cmdOutput := string(m)
if e != nil {
log.Println("error running command:", e.Error())
cmdOutput = cmdOutput + e.Error()
}
if cfg.Base64Encode {
cmdOutput = base64.StdEncoding.EncodeToString([]byte(cmdOutput))
}
pubMessage := piAliveMsg{
Timestamp: time.Now().String(),
Command: cfg.Command,
CommandOutput: cmdOutput,
}
x, _ := json.Marshal(pubMessage)
if token := c.Publish(topicPub, byte(cfg.Qos), cfg.Retained, x); token.Wait() && token.Error() != nil {
log.Println(token.Error())
} else {
log.Println("Published message")
}
time.Sleep(time.Duration(cfg.SendIntervalSec) * time.Second)
}
}()
time.Sleep(time.Duration(cfg.StartupPause) * time.Second)
}
```
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: 4679767cdd4d6fe3b9d1a93a07a573449f90c295
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.