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

qianlnk/superservice: superservicectl/command/command.go; 55 LoC

Open
#13,878 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 [qianlnk/superservice](https://www.github.com/qianlnk/superservice) at [superservicectl/command/command.go](https://github.com/qianlnk/superservice/blob/dcd95b4d9e3eae7e6a346c2f5fe56aefeb1c0ee2/superservicectl/command/command.go#L345-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 m used in defer or goroutine at line 363

[Click here to see the code in its original context.](https://github.com/qianlnk/superservice/blob/dcd95b4d9e3eae7e6a346c2f5fe56aefeb1c0ee2/superservicectl/command/command.go#L345-L399)

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

```go
for _, m := range machines {
if _, ok := ctl.ConnectMachineList[m]; ok {
fmt.Printf("WARNING: host '%s' already connected.", m)
}
if v, ok := ctl.ServiceMachineList[m]; ok {
go func() {
errcount := 0
for {
wsAddr := fmt.Sprintf("ws://%s:%s/Cmd", v.Host, v.Port)
httpAddr := fmt.Sprintf("http://%s:%s/Cmd?user=%s&password=%s", v.Host, v.Port, "qianlnk", "123456")
v.Ls = longsocket.NewConn(wsAddr, "", httpAddr, true, 128*1024)
err := v.Ls.Dial(true)
if err != nil {
errcount++
fmt.Println("err:", err)
}
if v.Ls.Status == longsocket.STATUS_INIT {
if errcount >= 2 {
delete(ctl.ConnectMachineList, m)
return
}
time.Sleep(2 * time.Second)
continue
}
ctl.ConnectMachineList[m] = v

reConn := make(chan bool)
go func() {
go v.Ls.ReadLoop()
go v.Ls.WriteLoop()
v.Ls.Read(dealResMessage)
reConn <- false
return
}()

select {
case <-v.CloseConn:
v.Ls.Close()
return
case <-reConn:
close(reConn)
break
}
}
}()
if _, ok := ctl.ConnectMachineList[m]; !ok {
time.Sleep(3 * time.Second)
if _, ok := ctl.ConnectMachineList[m]; !ok {
fmt.Printf("host '%s' connection refused.\n", m)
}
}
} else {
fmt.Printf("host '%s' is not exist.\n", m)
}
}

```

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

Contributor guide

No contributing guide indexed for this repository

Research direction

Read superservicectl/command/command.go around lines 345-399, focusing first on the range loop and the goroutines that use m and v. Check the analyzer finding for the loop-capture behavior and inspect the surrounding connection flow. Done means the reported finding is addressed without changing the intended connection handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.