{"record":{"id":"2f693ecfce9624bf","repo":"cilium/cilium","slug":"nil-pointer-to-t","errorCode":null,"errorMessage":"nil pointer to %T","messagePattern":"nil pointer to %T","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/bpf/analyze/fields.go","lineNumber":24,"sourceCode":"import (\n\t\"fmt\"\n\t\"reflect\"\n\n\t\"github.com/cilium/cilium/pkg/container/set\"\n)\n\n// This code is taken from ebpf-go while we figure out how to export it properly\n// from the library.\n\n// Fields extracts object names tagged 'ebpf' from a struct type.\nfunc Fields(to any) (*set.Set[string], error) {\n\ttoValue := reflect.ValueOf(to)\n\tif toValue.Type().Kind() != reflect.Pointer {\n\t\treturn nil, fmt.Errorf(\"%T is not a pointer to struct\", to)\n\t}\n\n\tif toValue.IsNil() {\n\t\treturn nil, fmt.Errorf(\"nil pointer to %T\", to)\n\t}\n\n\treturn ebpfFields(toValue.Elem(), nil)\n}\n\n// structField represents a struct field containing the ebpf struct tag.\ntype structField struct {\n\treflect.StructField\n\tvalue reflect.Value\n}\n\nfunc ebpfFields(structVal reflect.Value, visited map[reflect.Type]bool) (*set.Set[string], error) {\n\tif visited == nil {\n\t\tvisited = make(map[reflect.Type]bool)\n\t}\n\n\tstructType := structVal.Type()\n\tif structType.Kind() != reflect.Struct {","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/bpf/analyze/fields.go#L6-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"Nil results from failed unmarshal/constructor calls propagated into Fields; zero-value pointer fields; interface variables holding nil pointers.","solutions":["Initialize the struct before calling: cfg := &Config{}; Fields(cfg).","Check for nil before calling Fields, especially when the pointer comes from another function.","Fix upstream code that returns a nil pointer on error paths."],"exampleFix":"// before\nvar cfg *Config\nnames, err := analyze.Fields(cfg) // panics would follow, error here\n\n// after\ncfg := &Config{}\nnames, err := analyze.Fields(cfg)","handlingStrategy":"validation","validationCode":"if cfg == nil {\n    return fmt.Errorf(\"cannot analyze fields of nil *Config\")\n}\nnames, err := analyze.Fields(cfg)","typeGuard":"func nonNil[T any](p *T) bool { return p != nil }","tryCatchPattern":"names, err := analyze.Fields(cfg)\nif err != nil {\n    if strings.Contains(err.Error(), \"nil pointer\") {\n        return fmt.Errorf(\"initialize the struct before analysis: %w\", err)\n    }\n    return err\n}","preventionTips":["Initialize struct pointers with &T{} before passing to Fields.","Check for nil results from constructors/unmarshalers before further use.","Avoid passing zero-value pointer variables directly into reflection helpers."],"tags":["reflection","go","nil-pointer"],"backgroundTag":"nil-pointer-argument","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}