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

yolaadipratama/Tugas9: sup.go; 117 LoC

Open
#10,248 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 [yolaadipratama/Tugas9](https://www.github.com/yolaadipratama/Tugas9) at [sup.go](https://github.com/yolaadipratama/Tugas9/blob/fd96997162c2b41c90118db82899e26d3d700cec/sup.go#L121-L237)

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 170

[Click here to see the code in its original context.](https://github.com/yolaadipratama/Tugas9/blob/fd96997162c2b41c90118db82899e26d3d700cec/sup.go#L121-L237)

Click here to show the 117 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 {
var prefix string
var prefixLen int
if sup.prefix {
prefix, prefixLen = c.Prefix()
if len(prefix) < maxLen { // Left padding.
prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix
}
}

err := c.Run(task)
if err != nil {
return errors.Wrap(err, prefix+"task failed")
}

// 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, "%v", errors.Wrap(err, prefix+"reading STDOUT failed"))
}
}(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, "%v", errors.Wrap(err, prefix+"reading STDERR failed"))
}
}(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, "%v", errors.Wrap(err, "copying STDIN failed"))
}
// TODO: Use MultiWriteCloser (not in Stdlib), so we can writer.Close() instead?
for _, c := range clients {
c.WriteClose()
}
}()
}

// Catch OS signals and pass them to all active clients.
trap := make(chan os.Signal, 1)
signal.Notify(trap, os.Interrupt)
go func() {
for {
select {
case sig, ok := <-trap:
if !ok {
return
}
for _, c := range task.Clients {
err := c.Signal(sig)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", errors.Wrap(err, "sending signal failed"))
}
}
}
}
}()

// 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 {
var prefix string
if sup.prefix {
var prefixLen int
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, "%s%v\n", prefix, e)
os.Exit(e.ExitStatus())
}
fmt.Fprintf(os.Stderr, "%s%v\n", prefix, err)

// TODO: Shouldn't os.Exit(1) here. Instead, collect the exit statuses for later.
os.Exit(1)
}
}(c)
}

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

// Stop catching signals for the currently active clients.
signal.Stop(trap)
close(trap)
}

```

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

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.