Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(G602): report constant index equal to the length asserted by an e…
…quality guard

Under an "if len(s) == N" guard the *ssa.IndexAddr arm of the bounded
case suppressed the issue when the constant index equalled N. Valid
indices of a length-N slice are 0..N-1, so index == N is the one value
that is always out of range, and G602 silently dropped it.

Use isSliceIndexInsideBounds, as the sibling upperBounded arm already
does, and recover the asserted length when the compared expression
carries a constant offset ("len(s)-1 == 1" asserts a length of 2).

Fixes part of #1727
  • Loading branch information
askalf committed Sep 14, 2026
commit 2f088945f493a9954e329727c07f20109e081e60
21 changes: 20 additions & 1 deletion analyzers/slice_bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func runSliceBounds(pass *analysis.Pass) (result any, err error) {
}
case *ssa.IndexAddr:
if indexValue, ok := GetConstantInt64(tinstr.Index); ok {
if int(indexValue) == value {
if isSliceIndexInsideBounds(assertedLen(binop, value), int(indexValue)) {
delete(issues, instr)
}
}
Expand Down Expand Up @@ -910,6 +910,25 @@ func extractBinOpBound(binop *ssa.BinOp) (bound, int, error) {
return lowerUnbounded, 0, errExtractBinOp
}

// assertedLen returns the slice length asserted by an equality comparison whose
// constant side is value. The compared expression is usually len(s) itself, but
// it may carry a constant offset (e.g. "len(s) - 1 == 1" asserts a length of 2),
// in which case the offset has to be undone to recover the length.
func assertedLen(binop *ssa.BinOp, value int) int {
if binop == nil {
return value
}
for _, operand := range []ssa.Value{binop.X, binop.Y} {
if _, isConst := operand.(*ssa.Const); isConst {
continue
}
if _, offset := decomposeIndex(operand); offset != 0 {
return value - offset
}
}
return value
}

func isSliceIndexInsideBounds(h int, index int) bool {
return (0 <= index && index < h)
}
Expand Down
50 changes: 50 additions & 0 deletions testutils/g602_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,4 +740,54 @@ func main() {
}
}
`}, 1, gosec.NewConfig()},
// Issue #1727: G602 not reported for a constant index equal to the length
// asserted by an equality guard (index == len is always out of range)
{[]string{`
package main

import "fmt"

func main() {
s := make([]int, 0)
if len(s) == 3 {
fmt.Println(s[3])
}
}
`}, 1, gosec.NewConfig()},
{[]string{`
package main

import "fmt"

func main() {
s := make([]int, 0)
if len(s) == 3 {
fmt.Println(s[4])
}
}
`}, 1, gosec.NewConfig()},
{[]string{`
package main

import "fmt"

func main() {
s := make([]int, 0)
if len(s) == 3 {
fmt.Println(s[2])
}
}
`}, 0, gosec.NewConfig()},
{[]string{`
package main

import "fmt"

func main() {
s := make([]int, 0)
if len(s)-1 == 1 {
fmt.Println(s[1])
}
}
`}, 0, gosec.NewConfig()},
}