go-delve/delve · error
index out of bounds
Error message
index out of bounds
What it means
Indexing (a[i]) into a string, array, or slice is bounds-checked by Delve's evaluator before reading debuggee memory. This throw site is the pre-load bounds check: for normal variables the index must satisfy 0 <= i < v.Len; for C pointers (VariableCPtr flag) only i >= 0 is enforced since there is no known length.
Source
Thrown at pkg/proc/eval.go:2859
if tt1, isslice1 := t1.(*godwarf.SliceType); isslice1 {
tt2, isslice2 := t2.(*godwarf.SliceType)
if !isslice2 {
return false
}
return sameType(tt1.ElemType, tt2.ElemType)
}
return t1.String() == t2.String()
}
func (v *Variable) sliceAccess(idx int) (*Variable, error) {
wrong := false
if v.Flags&VariableCPtr == 0 {
wrong = idx < 0 || int64(idx) >= v.Len
} else {
wrong = idx < 0
}
if wrong {
return nil, errors.New("index out of bounds")
}
if v.loaded {
if v.Kind == reflect.String {
s := constant.StringVal(v.Value)
if idx >= len(s) {
return nil, errors.New("index out of bounds")
}
r := v.newVariable("", v.Base+uint64(int64(idx)*v.stride), v.fieldType, v.mem)
r.loaded = true
r.Value = constant.MakeInt64(int64(s[idx]))
return r, nil
} else {
if idx >= len(v.Children) {
return nil, errors.New("index out of bounds")
}
return &v.Children[idx], nil
}
}View on GitHub (pinned to a23773e6c3)
Solutions
- Check the length first: 'print len(s)' or 'print len(a)', then use an index in [0, len)
- If the variable shows an unexpectedly small Len, inspect why (is it empty? did you index the wrong variable?)
- Use slice syntax to see available range, e.g. 'print s[0:5]' or 'print a[0:3]'
- For C pointers, remember only negative indexes are rejected; bounds beyond the allocation are your responsibility
Example fix
// before (dlv CLI) (dlv) print s[10] // len(s) == 5 -> error // after (dlv) print len(s) 5 (dlv) print s[4]
Defensive patterns
Strategy: validation
Validate before calling
// before indexing in dlv (dlv) print len(s) // confirm length, then index in [0, len) (dlv) print s[4]
Type guard
func safeIndex(idx int, length int64) bool {
return idx >= 0 && int64(idx) < length
} Try / catch
// RPC client pattern
val, err := client.EvalVariable(scope, fmt.Sprintf("s[%d]", i), cfg)
if err != nil && strings.Contains(err.Error(), "index out of bounds") {
// query length and clamp/retry
lenVal, _ := client.EvalVariable(scope, "len(s)", cfg)
// recompute i within [0, len)
} Prevention
- Always check len() in the debugger before indexing
- Watch for loop variables that have advanced past the end when evaluating
- Remember empty slices/strings reject index 0
- For C pointers only negative indexes are checked — track your own bounds
When it happens
Trigger: Evaluating 'print s[10]' where s has Len <= 10 (string/slice/array), or a negative index like 'print a[-1]' on any indexable variable; also 'print p[5]' where p is a C pointer — only negative indexes fail here.
Common situations: Off-by-one errors while inspecting loops; using a loop variable that has already advanced past the end; indexing an empty slice/string (Len 0); mixing 0-based/1-based reasoning when poking at raw C pointers.
Related errors
- could not decode first frame
- unable to find function context
- unable to find locals: no debug information present in binar
- no address for escaped variable
- can not assign to function expression
AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31).
Data as JSON: /api/errors/bb1d02e683ff6a57.
Report an issue: GitHub.