{"record":{"id":"16439a6ae3fd6187","repo":"hyperledger/fabric","slug":"proto-marshal-called-with-nil-16439a","errorCode":null,"errorMessage":"proto: Marshal called with nil","messagePattern":"proto: Marshal called with nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/scc/lscc/lscc.go","lineNumber":327,"sourceCode":"\n\tchaincodeData := &ccprovider.ChaincodeData{}\n\terr = proto.Unmarshal(chaincodeDataBytes, chaincodeData)\n\tif err != nil {\n\t\t// this kind of data corruption is unexpected since our code\n\t\t// always marshals ChaincodeData into these keys\n\t\tunexpectedErr = errors.Wrapf(err, \"chaincode %s has bad definition\", chaincodeName)\n\t\treturn\n\t}\n\n\tplugin = chaincodeData.Vscc\n\targs = chaincodeData.Policy\n\treturn\n}\n\n// create the chaincode on the given chain\nfunc (lscc *SCC) putChaincodeData(stub shim.ChaincodeStubInterface, cd *ccprovider.ChaincodeData) error {\n\tif cd == nil {\n\t\treturn errors.New(\"proto: Marshal called with nil\")\n\t}\n\tcdbytes, err := proto.Marshal(cd)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif cdbytes == nil {\n\t\treturn MarshallErr(cd.Name)\n\t}\n\n\terr = stub.PutState(cd.Name, cdbytes)\n\n\treturn err\n}\n","sourceCodeStart":309,"sourceCodeEnd":342,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/scc/lscc/lscc.go#L309-L342","documentation":"putChaincodeData rejects a nil *ccprovider.ChaincodeData before attempting proto.Marshal, returning the same message the protobuf library uses for nil messages. LSCC requires a valid ChaincodeData to persist the chaincode definition to the ledger; passing nil is a programming error in the caller (e.g., executeChaincode/GetChaincodeToInstall flow constructed no chaincode data).","triggerScenarios":"Calling lscc.putChaincodeData (via LSCC deploy/instantiate processing) with cd == nil — typically when a deploy path fails to build a ChaincodeData from the deployment spec before persisting it.","commonSituations":"Custom lifecycle code or forks of lscc that construct ChaincodeData conditionally and skip error checks; upstream bug paths where an earlier parse of the ChaincodeDeploymentSpec returned no data but the flow continued to putChaincodeData.","solutions":["Ensure the ChaincodeData passed to putChaincodeData is fully populated (name, version, path, Vscc, Policy, Escc, Id) before persisting.","Check the caller that builds the ChaincodeData for early error returns that leave it nil; propagate those errors instead of continuing.","If patching/forking lscc, add the nil check (as upstream does) so the failure is explicit rather than a proto.Marshal panic-style error."],"exampleFix":"// before\ncd := buildChaincodeData(spec) // may return nil on parse failure\nlscc.putChaincodeData(stub, cd)\n// after\ncd := buildChaincodeData(spec)\nif cd == nil {\n    return fmt.Errorf(\"failed to build ChaincodeData for %s\", spec.ChaincodeSpec.ChaincodeId.Name)\n}\nlscc.putChaincodeData(stub, cd)","handlingStrategy":"validation","validationCode":"// guard the ChaincodeData before the LSCC persist path\nfunc validChaincodeData(cd *ccprovider.ChaincodeData) bool {\n    return cd != nil && cd.Name != \"\" && cd.Version != \"\" && cd.Vscc != \"\" && cd.Escc != \"\"\n}","typeGuard":"func isNilChaincodeData(cd *ccprovider.ChaincodeData) bool { return cd == nil || reflect.ValueOf(cd).IsNil() }","tryCatchPattern":"// Go: check error from the deploy path and report nil-data programming bugs\nif err := lscc.Invoke(stub); err != nil {\n    if strings.Contains(err.Error(), \"Marshal called with nil\") {\n        return fmt.Errorf(\"internal: ChaincodeData was nil before persist: %w\", err)\n    }\n    return err\n}","preventionTips":["Construct ChaincodeData via ccprovider helpers rather than ad-hoc structs.","Always check errors from the step that parses the deployment spec before persisting its data.","Add unit tests covering the nil-data branch of persist functions.","Keep LSCC fork/patch code in sync with upstream nil checks."],"tags":["hyperledger-fabric","lscc","protobuf","nil-check"],"backgroundTag":"nil-argument","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"}