go-delve/delve · error

nil can not be dereferenced

Error message

nil can not be dereferenced

What it means

evalPointerDeref evaluates *p. A nil pointer expression is represented by the nilVariable sentinel, which has no memory to dereference, so delve throws this instead of attempting a read. This is the debugger-side equivalent of Go's nil dereference, raised before any memory access.

Source

Thrown at pkg/proc/eval.go:2313

		}
		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) {
	xev := stack.pop()

	if xev.Kind != reflect.Ptr {
		stack.err = fmt.Errorf("expression %q (%s) can not be dereferenced", astutil.ExprToString(op.Node.X), xev.TypeString())
		return
	}

	if xev == nilVariable {
		stack.err = errors.New("nil can not be dereferenced")
		return
	}

	if len(xev.Children) == 1 {
		// this branch is here to support pointers constructed with typecasts from ints
		xev.Children[0].OnlyAddr = false
		stack.push(&(xev.Children[0]))
		return
	}
	xev.loadPtr()
	if xev.Unreadable != nil {
		val, ok := constant.Uint64Val(xev.Value)
		if ok && val == 0 {
			stack.err = fmt.Errorf("couldn't read pointer: %w", xev.Unreadable)
			return
		}
	}
	rv := &xev.Children[0]

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Check the pointer before dereferencing: print p != nil, then print *p only when non-nil.
  2. Use Go-style optional access in expressions where supported, or guard breakpoint conditions with `p != nil && *p == x`.
  3. Step to a point after initialization; the nil may be valid at the current stop point.
  4. In tooling, detect Kind == reflect.Ptr with Addr/children indicating nil and render '<nil>' instead of issuing the deref.

Example fix

// before
condition: "*ptr == 42"
// after
condition: "ptr != nil && *ptr == 42"
Defensive patterns

Strategy: type-guard

Validate before calling

pv, err := scope.EvalExpression(ptrExpr, cfg)
if err != nil { return err }
if pv.Kind == reflect.Ptr && (pv == proc.NilVariablePlaceholder() || pv.Addr == 0) {
    return fmt.Errorf("%s is nil; not dereferencing", ptrExpr)
}

Type guard

func dereferenceable(v *proc.Variable) bool {
    return v != nil && v.Kind == reflect.Ptr && v != nilSentinel
}

Try / catch

_, err := scope.EvalExpression("*"+ptrExpr, cfg)
if err != nil && strings.Contains(err.Error(), "nil can not be dereferenced") {
    return fmt.Errorf("%s is nil", ptrExpr)
}

Prevention

When it happens

Trigger: Evaluating *p where p == nil (e.g., *nil or *typedNil); dereferencing a pointer-typed expression that evaluated to the nil constant; print *s.PointerField when PointerField is nil.

Common situations: Inspecting a struct whose pointer field was never initialized; single-stepping before an allocation happened; conditions on breakpoint expressions that dereference possibly-nil pointers.

Related errors


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