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

gophergala2016/supbot: Godeps/_workspace/src/github.com/pressly/sup/sup.go; 83 LoC

Open
#5,943 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 [gophergala2016/supbot](https://www.github.com/gophergala2016/supbot) at [Godeps/_workspace/src/github.com/pressly/sup/sup.go](https://github.com/gophergala2016/supbot/blob/77bb0a281f3b453a5db003414b38bf28f8bed613/Godeps/_workspace/src/github.com/pressly/sup/sup.go#L104-L186)

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 task used in defer or goroutine at line 149

[Click here to see the code in its original context.](https://github.com/gophergala2016/supbot/blob/77bb0a281f3b453a5db003414b38bf28f8bed613/Godeps/_workspace/src/github.com/pressly/sup/sup.go#L104-L186)

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

```go
for _, task := range tasks {
var writers []io.Writer
var wg sync.WaitGroup

// Run tasks on the provided clients.
for _, c := range task.Clients {
prefix, prefixLen := c.Prefix()
if len(prefix) < maxLen { // Left padding.
prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix
}

err := c.Run(task)
if err != nil {
return fmt.Errorf("%s%v", prefix, err)
}

// Copy over tasks's STDOUT.
wg.Add(1)
go func(c Client) {
defer wg.Done()
_, err := io.Copy(os.Stdout, prefixer.New(c.Stdout(), prefix))
if err != nil && err != io.EOF {
// TODO: io.Copy() should not return io.EOF at all.
// Upstream bug? Or prefixer.WriteTo() bug?
fmt.Fprintf(os.Stderr, "%sSTDOUT: %v", prefix, err)
}
}(c)

// Copy over tasks's STDERR.
wg.Add(1)
go func(c Client) {
defer wg.Done()
_, err := io.Copy(os.Stderr, prefixer.New(c.Stderr(), prefix))
if err != nil && err != io.EOF {
fmt.Fprintf(os.Stderr, "%sSTDERR: %v", prefix, err)
}
}(c)

writers = append(writers, c.Stdin())
}

// Copy over task's STDIN.
if task.Input != nil {
go func() {
writer := io.MultiWriter(writers...)
_, err := io.Copy(writer, task.Input)
if err != nil && err != io.EOF {
fmt.Fprintf(os.Stderr, "STDIN: %v", err)
}
// TODO: Use MultiWriteCloser (not in Stdlib), so we can writer.Close() instead?
for _, c := range clients {
c.WriteClose()
}
}()
}

// Wait for all I/O operations first.
wg.Wait()

// Make sure each client finishes the task, return on failure.
for _, c := range task.Clients {
wg.Add(1)
go func(c Client) {
defer wg.Done()
if err := c.Wait(); err != nil {
prefix, prefixLen := c.Prefix()
if len(prefix) < maxLen { // Left padding.
prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix
}
if e, ok := err.(*ssh.ExitError); ok && e.ExitStatus() != 15 {
// TODO: Store all the errors, and print them after Wait().
fmt.Fprintf(os.Stderr, "%sexit %v\n", prefix, e.ExitStatus())
os.Exit(e.ExitStatus())
}
fmt.Fprintf(os.Stderr, "%s%v\n", prefix, err)
os.Exit(1)
}
}(c)
}

// Wait for all commands to finish.
wg.Wait()
}

```

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: 77bb0a281f3b453a5db003414b38bf28f8bed613

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.