Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 24 additions & 1 deletion analyzers/slice_bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,12 @@ func runSliceBounds(pass *analysis.Pass) (result any, err error) {
delete(issues, instr)
}
case *ssa.IndexAddr:
// invBound maps bounded to itself, so the "else" successor
// is walked with the same bound and value even though it
// only proves the length is *not* the asserted one. Trust
// the asserted length in the "then" successor alone.
if indexValue, ok := GetConstantInt64(tinstr.Index); ok {
if int(indexValue) == value {
if i == 0 && isSliceIndexInsideBounds(assertedLen(binop, value), int(indexValue)) {
delete(issues, instr)
}
}
Expand Down Expand Up @@ -910,6 +914,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
216 changes: 216 additions & 0 deletions testutils/g602_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,5 +739,221 @@ func main() {
accessed[i] = &r
}
}
`}, 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()},
{[]string{`
package main

import "fmt"

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

import "fmt"

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

import "fmt"

func main() {
s := make([]int, 0)
if len(s) == 0 {
fmt.Println(s[0])
}
}
`}, 1, gosec.NewConfig()},
// Reversed operand order: the constant sits on the left of the guard, which
// extractBinOpBound handles in its binop.X arm. The asserted length has to
// be honoured in the "then" branch and distrusted in the "else" branch just
// as it is when the constant is on the right.
{[]string{`
package main

import "fmt"

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

import "fmt"

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

import "fmt"

func main() {
s := make([]int, 0)
if 3 == len(s) {
} else {
fmt.Println(s[1])
}
}
`}, 1, gosec.NewConfig()},
// A positive constant offset on the compared expression: "len(s) + 1 == 4"
// asserts a length of 3, so index 3 is out of range and index 2 is not.
{[]string{`
package main

import "fmt"

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

import "fmt"

func main() {
s := make([]int, 0)
if len(s)+1 == 4 {
fmt.Println(s[2])
}
}
`}, 0, gosec.NewConfig()},
// An "else if" chain: the inner guard owns its own "then" successor, so the
// length it asserts still clears an index inside that length.
{[]string{`
package main

import "fmt"

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

import "fmt"

func main() {
s := make([]int, 0)
if len(s) == 3 {
} else if len(s) == 5 {
fmt.Println(s[5])
}
}
`}, 1, gosec.NewConfig()},
// A nested equality guard inside the outer "else" branch: the nested guard
// is authoritative for the block it opens, so an index inside the length it
// asserts is cleared even though the outer branch asserts nothing.
{[]string{`
package main

import "fmt"

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

import "fmt"

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