{"record":{"id":"d4102c095f235be4","repo":"cilium/cilium","slug":"clocksource-is-nil","errorCode":null,"errorMessage":"clockSource is nil","messagePattern":"clockSource is nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/maps/timestamp/timestamp.go","lineNumber":104,"sourceCode":"\t\treturn t / 1000000000, nil\n\tcase models.ClockSourceModeJiffies:\n\t\tj, err := probes.Jiffies()\n\t\tif err != nil {\n\t\t\treturn 0, err\n\t\t}\n\t\treturn j >> bpfMonoScaler, nil\n\tdefault:\n\t\treturn 0, fmt.Errorf(\"invalid clocksource: %s\", clockSource.Mode)\n\t}\n}\n\ntype TimestampConverter func(timestamp uint64) uint64\n\n// Returns a function that converts a CT timestamp from clocksource units into\n// seconds.\nfunc NewCTTimeToSecConverter(clockSource *models.ClockSource) (TimestampConverter, error) {\n\tif clockSource == nil {\n\t\treturn nil, fmt.Errorf(\"clockSource is nil\")\n\t}\n\tswitch clockSource.Mode {\n\tcase models.ClockSourceModeKtime:\n\t\tconverter := func(timestamp uint64) uint64 {\n\t\t\treturn timestamp\n\t\t}\n\t\treturn converter, nil\n\tcase models.ClockSourceModeJiffies:\n\t\thertz := clockSource.Hertz\n\t\tif hertz == 0 {\n\t\t\treturn nil, fmt.Errorf(\"invalid clock Hertz value (0)\")\n\t\t}\n\t\tconverter := func(timestamp uint64) uint64 {\n\t\t\treturn (timestamp << bpfMonoScaler) / uint64(hertz)\n\t\t}\n\t\treturn converter, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"invalid clocksource: %s\", clockSource.Mode)","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/maps/timestamp/timestamp.go#L86-L122","documentation":"NewCTTimeToSecConverter builds a timestamp-to-seconds converter based on the agent-reported clocksource. A nil clockSource argument gives no mode information, so the constructor returns this error immediately instead of dereferencing nil. It is a fail-fast guard for callers that skipped clocksource discovery.","triggerScenarios":"Passing nil to NewCTTimeToSecConverter — typically when GetClockSourceFromAgent already failed, an error path was ignored upstream, or a caller hardcodes nil assuming ktime.","commonSituations":"Ignoring the error from GetClockSourceFromAgent and using the nil result; wiring the converter before the agent health endpoint is available; tests passing nil stubs.","solutions":["Check and propagate the error from GetClockSourceFromAgent before constructing the converter","Fall back to reading the clocksource from the agent config file when the API returns none","Never pass a literal nil; validate the models.ClockSource at the call site"],"exampleFix":"// before: nil deref risk\nclockSource, _ := GetClockSourceFromAgent(svc)\nconv, err := NewCTTimeToSecConverter(clockSource)\n// after: check error and nil\nclockSource, err := GetClockSourceFromAgent(svc)\nif err != nil {\n    return nil, err\n}\nconv, err := NewCTTimeToSecConverter(clockSource)","handlingStrategy":"validation","validationCode":"// Guard before constructing the converter\nif clockSource == nil {\n    return errors.New(\"clocksource must be resolved (API or config) before building converter\")\n}","typeGuard":"func hasClockSource(cs *models.ClockSource) bool {\n    return cs != nil && cs.Mode != \"\"\n}","tryCatchPattern":"// Go: nil-check pattern\nclockSource, err := GetClockSourceFromAgent(svc)\nif err != nil {\n    return fmt.Errorf(\"cannot build time converter: %w\", err)\n}\nconv, err := NewCTTimeToSecConverter(clockSource)\nif err != nil {\n    return err\n}","preventionTips":["Never ignore the error from GetClockSourceFromAgent","Wire converter construction only after clocksource discovery succeeds","Avoid literal nil in tests; use a ktime ClockSource stub","Use nilness-style linters to catch unchecked error paths yielding nil"],"tags":["nil-argument","clocksource","api-misuse"],"backgroundTag":"nil-clocksource-argument","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}