cilium/cilium · error

nil pointer to %T

Error message

nil pointer to %T

What it means

Fields requires a non-nil pointer to struct. After confirming the argument is a pointer, it checks IsNil and fails with this error when the caller passed a nil pointer, since there is no struct value to inspect for 'ebpf' tags.

Source

Thrown at pkg/bpf/analyze/fields.go:24

import (
	"fmt"
	"reflect"

	"github.com/cilium/cilium/pkg/container/set"
)

// This code is taken from ebpf-go while we figure out how to export it properly
// from the library.

// Fields extracts object names tagged 'ebpf' from a struct type.
func Fields(to any) (*set.Set[string], error) {
	toValue := reflect.ValueOf(to)
	if toValue.Type().Kind() != reflect.Pointer {
		return nil, fmt.Errorf("%T is not a pointer to struct", to)
	}

	if toValue.IsNil() {
		return nil, fmt.Errorf("nil pointer to %T", to)
	}

	return ebpfFields(toValue.Elem(), nil)
}

// structField represents a struct field containing the ebpf struct tag.
type structField struct {
	reflect.StructField
	value reflect.Value
}

func ebpfFields(structVal reflect.Value, visited map[reflect.Type]bool) (*set.Set[string], error) {
	if visited == nil {
		visited = make(map[reflect.Type]bool)
	}

	structType := structVal.Type()
	if structType.Kind() != reflect.Struct {

View on GitHub (pinned to ac7b90affa)

Solutions

  1. Initialize the struct before calling: cfg := &Config{}; Fields(cfg).
  2. Check for nil before calling Fields, especially when the pointer comes from another function.
  3. Fix upstream code that returns a nil pointer on error paths.

Example fix

// before
var cfg *Config
names, err := analyze.Fields(cfg) // panics would follow, error here

// after
cfg := &Config{}
names, err := analyze.Fields(cfg)
Defensive patterns

Strategy: validation

Validate before calling

if cfg == nil {
    return fmt.Errorf("cannot analyze fields of nil *Config")
}
names, err := analyze.Fields(cfg)

Type guard

func nonNil[T any](p *T) bool { return p != nil }

Try / catch

names, err := analyze.Fields(cfg)
if err != nil {
    if strings.Contains(err.Error(), "nil pointer") {
        return fmt.Errorf("initialize the struct before analysis: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling analyze.Fields((*Config)(nil)) or passing an uninitialized struct pointer variable (var cfg *Config; Fields(cfg)); a function returned nil and its result was passed straight through.

Common situations: Nil results from failed unmarshal/constructor calls propagated into Fields; zero-value pointer fields; interface variables holding nil pointers.

Related errors


AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31). Data as JSON: /api/errors/2f693ecfce9624bf. Report an issue: GitHub.