github-vet / github-vet/rangeloop-pointer-findings
mayadata-io/d-operators: tools/d-action/pkg/action/shell_command.go; 101 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [mayadata-io/d-operators](https://www.github.com/mayadata-io/d-operators) at [tools/d-action/pkg/action/shell_command.go](https://github.com/mayadata-io/d-operators/blob/6206cd14b118f57adfa0bf094bb615c333756cb4/tools/d-action/pkg/action/shell_command.go#L136-L236)
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 rc used in defer or goroutine at line 186
[Click here to see the code in its original context.](https://github.com/mayadata-io/d-operators/blob/6206cd14b118f57adfa0bf094bb615c333756cb4/tools/d-action/pkg/action/shell_command.go#L136-L236)
Click here to show the 101 line(s) of Go which triggered the analyzer.
```go
for _, rc := range l.Items {
var stdout, stderr strings.Builder
var stoperr error
var warn string
var isTimeout bool
// default to global setting
timeoutInSecs = l.TimeoutInSeconds
if rc.TimeoutInSeconds != nil && *rc.TimeoutInSeconds > 0 {
timeoutInSecs = *rc.TimeoutInSeconds
}
// Disable output buffering, enable streaming
options := cmd.Options{
Buffered: false,
Streaming: true,
}
execCmd := cmd.NewCmdOptions(options, rc.CMD, rc.Args...)
// set environment variables
execCmd.Env = cmdEnv
doneChan := make(chan struct{})
// Capture STDOUT and STDERR lines streaming due to execution
// of command
go func() {
defer close(doneChan)
timeout := time.Duration(timeoutInSecs) * time.Second
timeoutChan := time.After(timeout) // does not block
for execCmd.Stdout != nil || execCmd.Stderr != nil {
select {
case line, open := <-execCmd.Stdout:
if !open {
execCmd.Stdout = nil
continue
}
// build the output
stdout.WriteString(line)
case line, open := <-execCmd.Stderr:
if !open {
execCmd.Stderr = nil
continue
}
// build the error
stderr.WriteString(line)
case <-timeoutChan:
klog.V(1).Infof(
"Command timed out: Name %q: Timeout %s",
rc.Name,
timeout.Round(time.Millisecond).String(),
)
isTimeout = true
execCmd.Stdout = nil
execCmd.Stderr = nil
stoperr = execCmd.Stop()
}
}
}()
// Run and wait for command to return
statusChan := <-execCmd.Start()
// Wait for goroutine to capture stdout &/or stderr
<-doneChan
if stoperr != nil {
// Any error while stopping the command is considered
// as a warning
warn = stoperr.Error()
}
timeTaken := time.Duration(statusChan.Runtime) * time.Second
timeTakenFmt := timeTaken.Round(time.Millisecond).String()
var statusChanErr = ""
if statusChan.Error != nil {
statusChanErr = statusChan.Error.Error()
}
output[rc.Name] = types.CommandOutput{
CMD: statusChan.Cmd,
Completed: statusChan.Complete,
Timedout: isTimeout,
Error: statusChanErr,
Exit: statusChan.Exit,
PID: statusChan.PID,
Stderr: stderr.String(),
Stdout: stdout.String(),
ExecutionTime: types.ExecutionTime{
ValueInSeconds: timeTaken.Seconds() + 0.0001,
ReadableValue: timeTakenFmt,
},
Warning: warn,
}
// check for errors
if isTimeout || stderr.Len() != 0 || statusChanErr != "" {
// verify if logic should continue or break out
if !l.ContinueOnError {
break
}
}
}
```
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: 6206cd14b118f57adfa0bf094bb615c333756cb4
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.