dgryski / semgrep-go

Go rules for semgrep and go-ruleguard
MIT License
457 stars 37 forks source link

Check against for x := range <-ch? #28

Open ainar-g opened 3 years ago

ainar-g commented 3 years ago

An interesting gotcha I found recently, which could be dangerous in overly-dynamically-typed code:

ch := make(chan []int, 3)
ch <- []int{10, 20, 30}
close(ch)

for s := range <-ch {
        fmt.Println(s)
}

As opposed to:

[10 20 30]

This prints:

0
1
2

Because what the user actually wanted to write is probably:

for s := range ch {
        fmt.Println(s)
}

Without the <-.