go-delve/delve · error

map index out of bounds

Error message

map index out of bounds

What it means

For map reslices, delve adds the low bound to an internal skip counter (mapSkip) and reads the map's length; if the skip count is >= the map length there are no entries to return, so this error is thrown. Unlike Go slices (which clamp), map 'slicing' in the debugger is bounds-checked.

Source

Thrown at pkg/proc/eval.go:2286

	}

	switch xev.Kind {
	case reflect.Slice, reflect.Array, reflect.String:
		if xev.Base == 0 {
			stack.err = fmt.Errorf("can not slice %q", astutil.ExprToString(op.Node.X))
			return
		}
		stack.pushErr(xev.reslice(low, high, op.TrustLen))
		return
	case reflect.Map:
		if op.Node.High != nil {
			stack.err = errors.New("second slice argument must be empty for maps")
			return
		}
		xev.mapSkip += int(low)
		xev.mapIterator(0) // reads map length
		if int64(xev.mapSkip) >= xev.Len {
			stack.err = errors.New("map index out of bounds")
			return
		}
		stack.push(xev)
		return
	case reflect.Ptr:
		if xev.Flags&VariableCPtr != 0 {
			stack.pushErr(xev.reslice(low, high, op.TrustLen))
			return
		}
		fallthrough
	default:
		stack.err = fmt.Errorf("can not slice %q (type %s)", astutil.ExprToString(op.Node.X), xev.TypeString())
		return
	}
}

// Evaluates a pointer dereference expression: *<subexpr>
func (scope *EvalScope) evalPointerDeref(op *evalop.PointerDeref, stack *evalStack) {

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Check len(m) first (print len(m)) and use a low bound smaller than the length.
  2. Use m[0:] or m[0:0] to view the map from the start.
  3. If the map should be non-empty, verify the program state — the map may have been drained or never populated; set a breakpoint where it is built.
  4. In tooling, guard reslice requests: only issue map reslice when requested low < variable.Len.

Example fix

// before
print m[10:] // m has 3 entries
// after
print m[0:]  // or check len(m) first
Defensive patterns

Strategy: validation

Validate before calling

mv, err := scope.EvalExpression(mapExpr, cfg)
if err != nil { return err }
if low >= int(mv.Len) {
    return fmt.Errorf("skip %d exceeds map length %d", low, mv.Len)
}

Type guard

func mapSkipInRange(mv *proc.Variable, low int) bool {
    return mv != nil && mv.Kind == reflect.Map && int64(low) < mv.Len
}

Try / catch

_, err := scope.EvalExpression(expr, cfg)
if err != nil && strings.Contains(err.Error(), "map index out of bounds") {
    return fmt.Errorf("skip index exceeds len(map); use %s[0:]", mapExpr)
}

Prevention

When it happens

Trigger: Evaluating m[n:] where n is greater than or equal to len(m); reslicing an empty map (m[0:]); using a stale length assumption after the map shrank between stops.

Common situations: Inspecting a map believed to be populated but actually empty; scripting that slices m[k:] with a computed k exceeding the live map size; skipping past all entries expecting remaining items.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/2223f0ad0cab0e7d. Report an issue: GitHub.