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

heyjohnnyfunt/tg-ping-bot: main.go; 55 LoC

Open
#12,632 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 [heyjohnnyfunt/tg-ping-bot](https://www.github.com/heyjohnnyfunt/tg-ping-bot) at [main.go](https://github.com/heyjohnnyfunt/tg-ping-bot/blob/0de9d7df755951caf5aca02ca06a4a1e18d7e551/main.go#L74-L128)

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 update used in defer or goroutine at line 119

[Click here to see the code in its original context.](https://github.com/heyjohnnyfunt/tg-ping-bot/blob/0de9d7df755951caf5aca02ca06a4a1e18d7e551/main.go#L74-L128)

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

```go
for update := range updates {

if update.Message == nil { // ignore any non-Message Updates
continue
}

var answer string

log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

words := strings.Fields(strings.ToLower(update.Message.Text))

if words[0] != "ping" {
continue
}

go func(bot *tgbotapi.BotAPI) {
if len(words) > 1 && len(words[1]) > 0 {

addr := words[1]
log.Printf("%s", addr)

if !httpsReg.MatchString(addr) {
addr = "http://" + addr
}

// check if IP or URL is valid
_, err1 := url.ParseRequestURI(addr)
err2 := net.ParseIP(addr) // returns nil if invalid

if err1 != nil && err2 == nil {
answer = "Give me valid IP or URL, SOAB!"
} else {
addr = httpsReg.ReplaceAllString(addr, "")
pingTime, err := Ping(addr)
if err != nil {
answer = "You think, I'm fool, SOAB, hah? This domain is bullshit!"
} else {
answer = fmt.Sprintf("time:%s\npong at:\n%s", pingTime, time.Now().UTC().String())
}
}
} else {
answer = fmt.Sprintf("pong at:\n%s", time.Now().UTC().String())
}

msg := tgbotapi.NewMessage(update.Message.Chat.ID, answer)
msg.ReplyToMessageID = update.Message.MessageID

_, err := bot.Send(msg)
if err != nil {
log.Printf("Can not send message: %s", err)
}
}(bot)

}

```

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: 0de9d7df755951caf5aca02ca06a4a1e18d7e551

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.