{"record":{"id":"a4530a72e68fe881","repo":"grpc/grpc-go","slug":"cannot-register-codecv2-with-empty-string-result-f","errorCode":null,"errorMessage":"cannot register CodecV2 with empty string result for Name()","messagePattern":"cannot register CodecV2 with empty string result for Name\\(\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"encoding/encoding_v2.go","lineNumber":68,"sourceCode":"// should match the content-subtype of the encoding handled by the CodecV2.  This\n// is case-insensitive, and is stored and looked up as lowercase.  If the\n// result of calling Name() is an empty string, RegisterCodecV2 will panic. See\n// Content-Type on\n// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for\n// more details.\n//\n// If both a Codec and CodecV2 are registered with the same name, the CodecV2\n// will be used.\n//\n// NOTE: this function must only be called during initialization time (i.e. in\n// an init() function), and is not thread-safe.  If multiple Codecs are\n// registered with the same name, the one registered last will take effect.\nfunc RegisterCodecV2(codec CodecV2) {\n\tif codec == nil {\n\t\tpanic(\"cannot register a nil CodecV2\")\n\t}\n\tif codec.Name() == \"\" {\n\t\tpanic(\"cannot register CodecV2 with empty string result for Name()\")\n\t}\n\tcontentSubtype := strings.ToLower(codec.Name())\n\tregisteredCodecs[contentSubtype] = codec\n}\n\n// GetCodecV2 gets a registered CodecV2 by content-subtype, or nil if no CodecV2 is\n// registered for the content-subtype.\n//\n// The content-subtype is expected to be lowercase.\nfunc GetCodecV2(contentSubtype string) CodecV2 {\n\tc, _ := registeredCodecs[contentSubtype].(CodecV2)\n\treturn c\n}\n","sourceCodeStart":50,"sourceCodeEnd":82,"githubUrl":"https://github.com/grpc/grpc-go/blob/0c51461d27177d997e14c642fe18c11668fc09a3/encoding/encoding_v2.go#L50-L82","documentation":"RegisterCodecV2 keys the codec by strings.ToLower(codec.Name()) exactly like RegisterCodec, so an empty Name() is rejected with a panic at encoding/encoding_v2.go:67-69. The name becomes the gRPC content-subtype and must be static.","triggerScenarios":"A CodecV2 whose Name() returns an unset field, an empty config-derived value, or a stubbed empty string.","commonSituations":"Porting a Codec to CodecV2 and leaving Name() stubbed; name sourced from config/flag that is empty in the deployment; copy-paste without renaming.","solutions":["Return a constant non-empty lowercase name from Name() (e.g. proto).","Validate any config-derived name is non-empty before constructing/registering the codec.","Keep the name identical across calls (it must be static)."],"exampleFix":"// before\nfunc (c myCodecV2) Name() string { return c.name } // empty -> panic\n\n// after\nfunc (c myCodecV2) Name() string { return \"myfmt\" }","handlingStrategy":"validation","validationCode":"func registerV2Safe(c encoding.CodecV2) {\n    if c == nil || c.Name() == \"\" {\n        log.Printf(\"skipping V2 registration: invalid name\")\n        return\n    }\n    encoding.RegisterCodecV2(c)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Return a constant non-empty lowercase name from CodecV2.Name().","Validate config-derived names before construction.","Keep the name static across calls."],"tags":["encoding","codecv2","registration","content-type","panic","go"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}