{"record":{"id":"2625db2f48a4288f","repo":"hyperledger/fabric","slug":"panic-err","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/configtx/util.go","lineNumber":29,"sourceCode":"\t\"github.com/hyperledger/fabric/protoutil\"\n\t\"google.golang.org/protobuf/proto\"\n)\n\n// UnmarshalConfig attempts to unmarshal bytes to a *cb.Config\nfunc UnmarshalConfig(data []byte) (*cb.Config, error) {\n\tconfig := &cb.Config{}\n\terr := proto.Unmarshal(data, config)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn config, nil\n}\n\n// UnmarshalConfigOrPanic attempts to unmarshal bytes to a *cb.Config or panics on error\nfunc UnmarshalConfigOrPanic(data []byte) *cb.Config {\n\tresult, err := UnmarshalConfig(data)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn result\n}\n\n// UnmarshalConfigUpdate attempts to unmarshal bytes to a *cb.ConfigUpdate\nfunc UnmarshalConfigUpdate(data []byte) (*cb.ConfigUpdate, error) {\n\tconfigUpdate := &cb.ConfigUpdate{}\n\terr := proto.Unmarshal(data, configUpdate)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn configUpdate, nil\n}\n\n// UnmarshalConfigUpdateOrPanic attempts to unmarshal bytes to a *cb.ConfigUpdate or panics on error\nfunc UnmarshalConfigUpdateOrPanic(data []byte) *cb.ConfigUpdate {\n\tresult, err := UnmarshalConfigUpdate(data)\n\tif err != nil {","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/configtx/util.go#L11-L47","documentation":"UnmarshalConfigOrPanic wraps UnmarshalConfig and panics instead of returning an error when the input bytes cannot be unmarshaled into a *cb.Config protobuf. This library uses *OrPanic variants for call sites where a failure indicates a programmer error or corrupted internal data that makes recovery impossible. The panic carries the underlying unmarshal error from the protobuf layer.","triggerScenarios":"Calling UnmarshalConfigOrPanic with nil bytes, truncated data, non-Config protobuf bytes, or bytes corrupted after a version change of the cb.Config message schema.","commonSituations":"Reading a config block from a badly corrupted or hand-edited ledger file; passing the wrong envelope field (e.g. ConfigUpdate bytes instead of Config bytes) to the helper; upgrading Fabric versions where stored config serialization assumptions changed.","solutions":["Validate the bytes with UnmarshalConfig (the non-panic variant) first and handle the error instead of using the OrPanic wrapper","Verify the input is the Config field from a ConfigEnvelope, not ConfigUpdate or envelope bytes","Re-fetch the config block from a trusted source (orderer/peer) rather than a local copy that may be corrupt","If the bytes come from a configtxgen-generated file, regenerate it with the matching Fabric version"],"exampleFix":"// before\nconfig := configtx.UnmarshalConfigOrPanic(rawBytes)\n// after\nconfig, err := configtx.UnmarshalConfig(rawBytes)\nif err != nil {\n    return fmt.Errorf(\"invalid config bytes: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if len(data) == 0 {\n    return errors.New(\"config bytes are empty\")\n}\nprobe := &cb.Config{}\nif err := proto.Unmarshal(data, probe); err != nil {\n    return fmt.Errorf(\"not a valid Config: %w\", err)\n}","typeGuard":"func looksLikeConfig(data []byte) bool {\n    probe := &cb.Config{}\n    return len(data) > 0 && proto.Unmarshal(data, probe) == nil\n}","tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        err = fmt.Errorf(\"UnmarshalConfigOrPanic failed: %v\", r)\n    }\n}()\nconfig := configtx.UnmarshalConfigOrPanic(data)","preventionTips":["Prefer the error-returning UnmarshalConfig in application code; reserve OrPanic for internal invariants","Never pass user-supplied bytes directly to the OrPanic variant","Validate payload header type before unmarshaling config data","Keep Fabric versions aligned across tools that produce and consume config bytes"],"tags":["go","fabric","protobuf","panic","deserialization"],"backgroundTag":"protobuf-unmarshal-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}