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

Comcast/sheens: tools/expect/expect.go; 176 LoC

Open
#16,254 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 [Comcast/sheens](https://www.github.com/Comcast/sheens) at [tools/expect/expect.go](https://github.com/Comcast/sheens/blob/68cbf968ea650eb52190cba24f9adddf20d60fc3/tools/expect/expect.go#L206-L381)

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 iop used in defer or goroutine at line 232

[Click here to see the code in its original context.](https://github.com/Comcast/sheens/blob/68cbf968ea650eb52190cba24f9adddf20d60fc3/tools/expect/expect.go#L206-L381)

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

```go
for _, iop := range s.IOs {

if iop.Timeout == 0 {
iop.Timeout = s.DefaultTimeout
}

var (
timer *time.Timer
errs = make(chan error, 3) // At least three

happy = errors.New("happy")
timeout = errors.New("timeout")
canceled = errors.New("canceled")
)

if 0 < iop.Timeout {
timer = time.AfterFunc(iop.Timeout, func() {
errs <- timeout
})
}

// Process stdout.
go func() {
f := func() error {

need := 0
for _, o := range iop.OutputSet {
if !o.Inverted {
need++
}
}

for {
line, err := out.ReadBytes('\n')
if err != nil {
return err
}

if s.ShowStdout {
log.Printf("out %s", line)
}

var message interface{}
if err = json.Unmarshal(line, &message); err != nil {
log.Printf("ignoring %s", line)
continue
} else {
for _, output := range iop.OutputSet {
if output.Bindingss != nil {
continue
}
var pattern = output.Pattern
var js []byte
if s.ParsePatterns {
js = []byte(pattern.(string))
if err = json.Unmarshal(js, &pattern); err != nil {
return fmt.Errorf("Unmarshal error %v for %s", err, js)
}
} else {
if js, err = json.Marshal(&pattern); err != nil {
return err
}
}

bss, err := match.Match(pattern, message, match.NewBindings())
if err != nil {
return err
}
if bss != nil {
if 1 < len(bss) {
log.Printf("warning: multiple Bindingss")
}

if output.GuardSource != nil {
if output.Guard, err = output.GuardSource.Compile(ctx, s.Interpreters); err != nil {
return err
}
}

if output.Guard != nil {
exe, err := output.Guard.Exec(ctx, bss[0], nil)
if err != nil {
return err
}
bss = []match.Bindings{exe.Bs}
}
}
if bss != nil {
output.Bindingss = bss
if output.Inverted {
return fmt.Errorf("undesired output %s", JS(output))
}
need--
}
}
if need == 0 {
return nil
}
}
}
}

err := f()
if timer != nil {
timer.Stop()
}
if err == nil {
errs <- happy
} else {
errs <- err
}
}()

// Send messages to stdin.
go func() {

f := func() error {
s.pause("waitBefore", iop.WaitBefore)

for i, input := range iop.Inputs {
if 0 < i {
s.pause("waitBetween", iop.WaitBetween)
}
js := []byte(input.(string))

if s.ShowStdin {
log.Printf("in %s\n", js)
}

if _, err := stdin.Write(js); err != nil {
return err
}

if _, err := stdin.Write(newline); err != nil {
return err
}
}

s.pause("waitAfter", iop.WaitAfter)
return nil
}

if err := f(); err == nil {
errs <- happy
} else {
errs <- err
}

}()

happies := 0
want := 2

LOOP:
for {

select {
case <-ctx.Done():
return canceled
case err = <-errs:
switch err {
case happy:
happies++
if want <= happies {
break LOOP
}
default:
break LOOP
}
}
}

if happies < want {
return err
}
}

```

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: 68cbf968ea650eb52190cba24f9adddf20d60fc3

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.