#1753·gosec

G602 does not check bounds when indexing into an append() result

Author: cipherprofessorCreated Sep 19, 2026Updated Sep 19, 2026

Split out from the append-related part of #1727 (originally raised by @pmkolo) since it's a distinct, more significant gap than the rest of that issue -- worth tracking separately.

Summary

G602 performs no bounds checking at all -- with or without any guard -- when the slice being indexed is the direct result of append().

Reproduction

go
package main

import "fmt"

func main() {
	s_int := []int{}
	s_int = append(s_int, 10)
	fmt.Println("s_int len: ", len(s_int))
	fmt.Println("s_int cap: ", cap(s_int))
	if len(s_int) == 3 {
		fmt.Println("s_int == ", s_int[6])
	}
	if len(s_int) >= 3 {
		fmt.Println("s_int >= ", s_int[6])
	}
	fmt.Println("s_int no guard ", s_int[6])

	s_int2 := []int{10}
	if len(s_int2) >= 3 {
		fmt.Println("s_int2 >= ", s_int2[6])
	}
	if len(s_int) >= 3 {
		fmt.Println("s_int2 with s_int guard ", s_int2[6])
	}
}

Ran against current master: only the last case fires (s_int2 indexed under s_int's unrelated guard -- correctly flagged, cross-variable-guard mismatch detection already works). None of the three s_int[6] accesses are flagged, not even the completely unguarded one on the line right after the guarded cases, despite s_int having length 1 at that point and index 6 being unconditionally out of range.

As @pmkolo noted, this isn't specific to append() changing the slice's underlying address -- the same loss of tracking happens with s_int := make([]int, 0, 4) (no append() involved at all), so the common factor is a slice whose value comes from something other than the two bases G602 currently recognizes.

Root cause

Dumped the SSA for the minimal case:

go
s := []int{}
s = append(s, 10)
fmt.Println(s[6])
t0 = new [0]int (slicelit)      // *ssa.Alloc
t1 = slice t0[:]                // *ssa.Slice
...
t5 = append(t1, t4...)          // *ssa.Call  <-- s_int after append()
t6 = &t5[6:int]                 // *ssa.IndexAddr, X = t5

analyzers/slice_bounds.go's dispatch on an IndexAddr's base (instr.X.(type)) only has cases for *ssa.Const (nil slices) and *ssa.Alloc (fixed-size arrays backing make()/composite literals). t5, the append() call's result, is a *ssa.Call -- there's no matching case for it at all, so t6 never enters any bounds-checking path, regardless of any guard. This is a distinct gap from the nil-slice/equality-guard inconsistency fixed in #1752 and the equality-guard work in #1746/#1749 -- none of that logic is even reached here, since the dispatch never gets past the base-type switch for this case.

Scope note

A real fix likely means: when an IndexAddr's base is an append() call whose arguments allow computing a statically-known resulting length (e.g. appending a fixed, small number of scalar arguments to a slice with an already-known length/cap, not a ... spread of another slice of unknown length), compute that length and route it through the same bounds-checking + guard-correlation machinery the Alloc case already uses. This is more involved than the recent nil-slice fix -- flagging it here for visibility/prioritization discussion before anyone invests implementation time, rather than assuming it's a quick fix.