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

LazarenkoA/TelegramBot: main.go; 109 LoC

Open
#17,212 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [LazarenkoA/TelegramBot](https://www.github.com/LazarenkoA/TelegramBot) at [main.go](https://github.com/LazarenkoA/TelegramBot/blob/b05c9d72c2d1d2fe764c0fcaca07fdefce1e7054/main.go#L164-L272)

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 255

[Click here to see the code in its original context.](https://github.com/LazarenkoA/TelegramBot/blob/b05c9d72c2d1d2fe764c0fcaca07fdefce1e7054/main.go#L164-L272)

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

```go
for update := range updates {
var Command string

//isGroup := update.Message != nil && update.Message.Chat.IsGroup()
//if isGroup {
// tf.Group().Initialise(bot, &update, func() {}).Start()
// continue
//}

u := &update
if !authorization(u, bot, Tasks) {
continue
}

if update.CallbackQuery != nil {
existNew := false
for _, t := range Tasks.GetTasks(update.CallbackQuery.From.ID) {
if t.GetState() != tel.StateDone {
callback := t.GetCallBack()
call := callback[update.CallbackQuery.Data]
if call != nil {
call()
}
existNew = true
}
}
if !existNew {
bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "Не найдено активных заданий."))
}
continue
}

if update.Message == nil {
continue
}

Command = update.Message.Command()
logrus.WithFields(logrus.Fields{
"Command": Command,
"Msg": update.Message.Text,
"FirstName": update.Message.From.FirstName,
"LastName": update.Message.From.LastName,
"UserName": update.Message.From.UserName,
"ChatID": update.Message.Chat.ID,
}).Debug()

fromID := update.Message.From.ID
// Чистим старые задания
Tasks.Delete(fromID)

var task tel.ITask
switch Command {
case "start":
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Привет %v %v!", update.Message.From.FirstName, update.Message.From.LastName)))
case "cancel":
//Tasks.Reset(fromID, bot, &update, true)
//bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Готово!"))
default:
if task = Tasks.Reuse(Command, fromID); task != nil {
break
}

if task = commandHandler(Command, mu); task != nil {
Tasks.AppendTask(task, fromID)
} else {
// Проверяем общие хуки
if Tasks.ExecuteHook(&update) {
continue
}

// Сохренение файла. Обязательно асинхронно
messageID := update.Message.MessageID
message := update.Message
go func() {
var msg tgbotapi.MessageConfig
if err := saveFile(message, bot); err != nil {
msg = tgbotapi.NewMessage(message.Chat.ID, "Я такому необученный.")
msg.ReplyToMessageID = messageID
} else {
msg = tgbotapi.NewMessage(message.Chat.ID, "👍🏻")
msg.ReplyToMessageID = messageID
}
bot.Send(msg)
}()
}
}
if task != nil {
// горутина нужна из-за Lock
go func() {
task.Lock(func() {
txt := fmt.Sprintf("Kоманда %q является эксклюзивной (параллельно несколько аналогичных команд выполняться не могут). Дождитесь окончания работы предыдущей команды", task.GetName())
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, txt))
})

// if race.Enabled {
// bot.Send(tgbotapi.NewMessage(task.GetChatID(), fmt.Sprintf("Коданда %q является эксклюзивной.\n Дождитесь завершения работы предыдущей команды", task.GetName())))
// }
task.InfoWrapper(task.Initialise(bot, &update, func() {
task.SetState(tel.StateDone)
msg := tgbotapi.NewMessage(task.GetChatID(), fmt.Sprintf("Задание:\n%v\nГотово!", task.GetDescription()))
msg.ReplyToMessageID = task.CurrentStep().GetMessageID()
bot.Send(msg)

Tasks.Delete(fromID)
task.Unlock()
}))
}()
}
}

```

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: b05c9d72c2d1d2fe764c0fcaca07fdefce1e7054

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.