G602 - nil slices, len guards with equality check, post-append slice checks
Author: pmkoloCreated Aug 24, 2026Updated Sep 19, 2026
While running gosec on inherited project I discovered discrepancy in slice bound checks
- nil slice immediately fires out of range issue, guards not checked
- empty, non-empty slices - the bound check on == appears incorrect
- append on slice - this looks like lost tracking in the analyzer so never fires like the s_int3 example, even though the bound check is for a different variable
package main
import (
"fmt"
)
func main() {
var s_int1 []int
fmt.Println("s_int1 len: ", len(s_int1))
if len(s_int1) == 3 {
fmt.Println("s_int1 == ", s_int1[2]) // <-- G602
}
if len(s_int1) >= 3 {
fmt.Println("s_int1 >= ", s_int1[2]) // <-- G602
}
s_int2 := []int{}
fmt.Println("s_int2 len: ", len(s_int2))
if len(s_int2) == 3 {
fmt.Println("s_int2 == ", s_int2[2]) // <-- G602
}
if len(s_int2) >= 3 {
fmt.Println("s_int2 >= ", s_int2[2])
}
s_int3 := []int{10}
fmt.Println("s_int3 len: ", len(s_int3))
if len(s_int3) == 3 {
fmt.Println("s_int3 == ", s_int3[2]) // <-- G602
}
if len(s_int3) >= 3 {
fmt.Println("s_int3 >= ", s_int3[2])
}
s_int4 := []int{}
s_int4 = append(s_int4, 10)
fmt.Println("s_int4 len: ", len(s_int4))
if len(s_int3) == 3 {
fmt.Println("s_int4 == ", s_int4[2])
}
if len(s_int3) >= 3 {
fmt.Println("s_int4 >= ", s_int4[2])
}
}Source: securego/gosec