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

kiyor/mssh: ssh.go; 68 LoC

Open
#15,688 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 [kiyor/mssh](https://www.github.com/kiyor/mssh) at [ssh.go](https://github.com/kiyor/mssh/blob/0547c406881facc504989e0a08dbe9d8f8479c90/ssh.go#L142-L209)

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 conn used in defer or goroutine at line 161

[Click here to see the code in its original context.](https://github.com/kiyor/mssh/blob/0547c406881facc504989e0a08dbe9d8f8479c90/ssh.go#L142-L209)

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

```go
for _, conn := range conns {
wg.Add(1)
*count++
if conf.Debug {
color.Printf("@{y}%s\t\tcounter %3d\n", conn, *count)
}
for *count >= conf.Workers {
time.Sleep(10 * time.Millisecond)
}
go func(h string) {
defer wg.Done()
var r Result

r.Host = h
client, err := ssh.Dial("tcp", h, config)
if err != nil {
color.Printf("@{!r}%s: Failed to connect: %s\n", h, err.Error())
*count--
if conf.Debug {
color.Printf("@{y}%s\t\tcounter %3d\n", conn, *count)
}
return
}

session, err := client.NewSession()
if err != nil {
color.Printf("@{!r}%s: Failed to create session: %s\n", h, err.Error())
*count--
if conf.Debug {
color.Printf("@{y}%s\t\tcounter %3d\n", conn, *count)
}
return
}
defer session.Close()

/* not working */
/* session.Setenv("PATH", "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin") */

var b bytes.Buffer
var e bytes.Buffer
session.Stdout = &b
session.Stderr = &e
if err := session.Run(command); err != nil {
color.Printf("@{!r}%s: Failed to run: %s\n", h, err.Error())
color.Printf("@{!r}%s\n", strip(e.String()))
*count--
if conf.Debug {
color.Printf("@{y}%s\t\tcounter %3d\n", conn, *count)
}
return
}
if !conf.Background {
r.Res = strip(b.String())
} else {
r.Res = "command success sent and out put in remote server's ~/nohup.out"
}
color.Printf("@{!g}%s\n", r.Host)
fmt.Println(r.Res)

*count--
if conf.Debug {
color.Printf("@{y}%s\t\tcounter %3d\n", conn, *count)
}

runtime.Gosched()
queue <- r
}(conn)
}

```

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: 0547c406881facc504989e0a08dbe9d8f8479c90

Contributor guide

No contributing guide indexed for this repository

Research direction

Read ssh.go lines 142-209, especially the range loop and goroutine containing the analyzer warning about conn. Determine whether the captured range variable is a real issue, then classify it by leaving the Bug, Mitigated, or Desirable Behavior reaction described in the issue.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.