{"record":{"id":"6782be926aa21c00","repo":"ipfs/kubo","slug":"panic-err-6782be","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"core/coreiface/idfmt.go","lineNumber":10,"sourceCode":"package iface\n\nimport (\n\t\"github.com/libp2p/go-libp2p/core/peer\"\n\tmbase \"github.com/multiformats/go-multibase\"\n)\n\nfunc FormatKeyID(id peer.ID) string {\n\tif s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil {\n\t\tpanic(err)\n\t} else {\n\t\treturn s\n\t}\n}\n\n// FormatKey formats the given IPNS key in a canonical way.\nfunc FormatKey(key Key) string {\n\treturn FormatKeyID(key.ID())\n}\n","sourceCodeStart":1,"sourceCodeEnd":20,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/coreiface/idfmt.go#L1-L20","documentation":"`FormatKeyID` panics when it cannot render a peer ID's CID form in base36. Peer IDs are internally converted to an identity-multihash CID; `peer.ToCid(id).StringOfBase(mbase.Base36)` can only fail for multibase/multihash encoding errors, which for valid peer IDs should be impossible — so the helper treats failure as a programming bug and panics rather than returning an error.","triggerScenarios":"Calling `FormatKeyID` (directly or via `FormatKey`) with a peer.ID that fails CID conversion or base36 encoding — in practice only reachable with a corrupt/invalid peer.ID value (e.g. an incorrectly deserialized or hand-crafted ID whose multihash cannot be encoded).","commonSituations":"Embedding kubo/coreiface and constructing peer.ID values from raw bytes that are not valid multihashes; decoding bugs where a key ID string was corrupted before being parsed back into a peer.ID; fuzzing or tests exercising invalid peer ID shapes.","solutions":["Ensure the peer.ID comes from a trusted source (parsed via peer.Decode or returned by libp2p APIs) before passing it to FormatKeyID/FormatKey.","If you cannot guarantee validity, wrap the call with recover() or validate the ID by round-tripping peer.ToCid(id).String() first and checking the error.","In library code you control, prefer a non-panicking variant that returns the error from StringOfBase instead of panicking."],"exampleFix":"// before\ns := iface.FormatKeyID(id) // panics on invalid id\n// after\nfunc safeFormatKeyID(id peer.ID) (s string, err error) {\n    defer func() { if r := recover(); r != nil { err = fmt.Errorf(\"invalid peer id: %v\", r) } }()\n    return iface.FormatKeyID(id), nil\n}","handlingStrategy":"try-catch","validationCode":"// Validate the peer ID before formatting:\nif _, err := peer.ToCid(id).String(); err != nil {\n    return fmt.Errorf(\"invalid peer id: %w\", err)\n}","typeGuard":"func validPeerID(id peer.ID) bool {\n    _, err := peer.ToCid(id).StringOfBase(mbase.Base36)\n    return err == nil\n}","tryCatchPattern":"func formatKeyIDSafe(id peer.ID) (s string, err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"FormatKeyID panicked: %v\", r)\n        }\n    }()\n    return iface.FormatKeyID(id), nil\n}","preventionTips":["Only pass peer.ID values from trusted libp2p APIs (peer.Decode, peer.ID(s))","Round-trip validate IDs parsed from external input before formatting","Wrap panic-prone formatters with recover() at library boundaries","Add tests covering malformed peer ID inputs if you handle user-supplied keys"],"tags":["panic","peer-id","ipns","coreapi"],"backgroundTag":"invalid-peer-id","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}