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

evergreen-ci/evergreen: agent/command.go; 80 LoC

Open
#9,517 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 [evergreen-ci/evergreen](https://www.github.com/evergreen-ci/evergreen) at [agent/command.go](https://github.com/evergreen-ci/evergreen/blob/a30fb318ff554bfa7becbbbc3d5ad63e356be156/agent/command.go#L65-L144)

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 cmd used in defer or goroutine at line 127

[Click here to see the code in its original context.](https://github.com/evergreen-ci/evergreen/blob/a30fb318ff554bfa7becbbbc3d5ad63e356be156/agent/command.go#L65-L144)

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

```go
for idx, cmd := range cmds {
if ctx.Err() != nil {
grip.Error("runCommands canceled")
return errors.New("runCommands canceled")
}
if cmd.DisplayName() == "" {
grip.Critical(message.Fields{
"message": "attempting to run an undefined command",
"name": cmd.Name(),
"type": cmd.Type(),
"raw": fmt.Sprintf("%#v", cmd),
"info": commandInfo,
})
}

// SetType implementations only modify the
// command's type *if* the command's type is
// not otherwise set.
cmd.SetType(tc.taskConfig.Project.CommandType)
cmd.SetJasperManager(a.jasper)

fullCommandName := getCommandName(commandInfo, cmd)

if !commandInfo.RunOnVariant(tc.taskConfig.BuildVariant.Name) {
tc.logger.Task().Infof("Skipping command %s on variant %s (step %d of %d)",
fullCommandName, tc.taskConfig.BuildVariant.Name, index, total)
continue
}

if len(cmds) == 1 {
tc.logger.Task().Infof("Running command %s (step %d of %d)", fullCommandName, index, total)
} else {
// for functions with more than one command
tc.logger.Task().Infof("Running command %v (step %d.%d of %d)", fullCommandName, index, idx+1, total)
}
for key, val := range commandInfo.Vars {
var newVal string
newVal, err = tc.taskConfig.Expansions.ExpandString(val)
if err != nil {
return errors.Wrapf(err, "Can't expand '%v'", val)
}
tc.taskConfig.Expansions.Put(key, newVal)
}

if options.isTaskCommands {
tc.setCurrentCommand(cmd)
tc.setCurrentIdleTimeout(cmd)
a.comm.UpdateLastMessageTime()
} else {
tc.setCurrentIdleTimeout(nil)
}

start := time.Now()
// We have seen cases where calling exec.*Cmd.Wait() waits for too long if
// the process has called subprocesses. It will wait until a subprocess
// finishes, instead of returning immediately when the context is canceled.
// We therefore check both if the context is cancelled and if Wait() has finished.
cmdChan := make(chan error, 1)
go func() {
defer func() {
// this channel will get read from twice even though we only send once, hence why it's buffered
cmdChan <- recovery.HandlePanicWithError(recover(), nil,
fmt.Sprintf("problem running command '%s'", cmd.Name()))
}()
cmdChan <- cmd.Execute(ctx, a.comm, logger, tc.taskConfig)
}()
select {
case err = <-cmdChan:
if err != nil {
tc.logger.Task().Errorf("Command failed: %v", err)
if options.isTaskCommands || options.shouldSetupFail {
return errors.Wrap(err, "command failed")
}
}
case <-ctx.Done():
tc.logger.Task().Errorf("Command stopped early: %s", ctx.Err())
return errors.Wrap(ctx.Err(), "Agent stopped early")
}
tc.logger.Task().Infof("Finished %s in %s", fullCommandName, time.Since(start).String())
}

```

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

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.