{"record":{"id":"afcf90c722de9a37","repo":"hyperledger/fabric","slug":"depspec-cannot-be-nil-if-buf-is-not-nil-afcf90","errorCode":null,"errorMessage":"depspec cannot be nil if buf is not nil","messagePattern":"depspec cannot be nil if buf is not nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/common/ccprovider/sigcdspackage.go","lineNumber":290,"sourceCode":"\tif _, err = ccpack.InitFromBuffer(buf); err != nil {\n\t\treturn nil, nil, err\n\t}\n\n\treturn ccpack.buf, ccpack.depSpec, nil\n}\n\n// PutChaincodeToFS - serializes chaincode to a package on the file system\nfunc (ccpack *SignedCDSPackage) PutChaincodeToFS() error {\n\tif ccpack.buf == nil {\n\t\treturn errors.New(\"uninitialized package\")\n\t}\n\n\tif ccpack.id == nil {\n\t\treturn errors.New(\"id cannot be nil if buf is not nil\")\n\t}\n\n\tif ccpack.sDepSpec == nil || ccpack.depSpec == nil {\n\t\treturn errors.New(\"depspec cannot be nil if buf is not nil\")\n\t}\n\n\tif ccpack.env == nil {\n\t\treturn errors.New(\"env cannot be nil if buf and depspec are not nil\")\n\t}\n\n\tif ccpack.data == nil {\n\t\treturn errors.New(\"nil data\")\n\t}\n\n\tif ccpack.datab == nil {\n\t\treturn errors.New(\"nil data bytes\")\n\t}\n\n\tccname := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Name\n\tccversion := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Version\n\n\t// return error if chaincode exists","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/common/ccprovider/sigcdspackage.go#L272-L308","documentation":"PutChaincodeToFS finally requires both sDepSpec and depSpec to be non-nil when buf is set, and further that env is non-nil. These are the parsed structures derived from the package bytes; missing ones indicate incomplete initialization. The library refuses to persist a partially parsed package.","triggerScenarios":"PutChaincodeToFS on a package where buf and id are set but the signed/unsigned deployment specs (or env) were never populated by InitFromBuffer — typically manual struct assembly.","commonSituations":"Custom install tooling that copies fields between SignedCDSPackage instances; skipped or failed InitFromBuffer steps whose errors were ignored; fabric version migrations.","solutions":["Use the full initialization flow (GetCCPackage -> InitFromBuffer) before persisting","Ensure InitFromBuffer ran to completion so sDepSpec, depSpec and env are all populated","Stop ignoring errors from earlier init steps; abort the install on any init failure"],"exampleFix":"// before\nccpack.buf = buf; ccpack.id = id // sDepSpec/depSpec/env nil\nccpack.PutChaincodeToFS()\n// after\nccpack := &SignedCDSPackage{}\nif _, err := ccpack.InitFromBuffer(buf); err != nil { return err } // populates all fields\nccpack.PutChaincodeToFS()","handlingStrategy":"type-guard","validationCode":"func ensureInitBeforePersist(ccpack *ccprovider.SignedCDSPackage, buf []byte) error {\n    if _, err := ccpack.InitFromBuffer(buf); err != nil {\n        return err // populates buf, id, sDepSpec, depSpec, env\n    }\n    return nil\n}","typeGuard":"func allFieldsSet(ccpack *ccprovider.SignedCDSPackage) bool {\n    b, _, err := ccpack.GetDepSpec()\n    return err == nil && b != nil\n}","tryCatchPattern":"if _, err := ccpack.InitFromBuffer(buf); err != nil {\n    return fmt.Errorf(\"cannot persist: init incomplete: %w\", err)\n}\nif err := ccpack.PutChaincodeToFS(); err != nil {\n    if strings.Contains(err.Error(), \"cannot be nil\") { return errors.New(\"partially initialized package; use InitFromBuffer first\") }\n    return err\n}","preventionTips":["Treat SignedCDSPackage as opaque: init via InitFromBuffer/GetCCPackage only","Never copy individual fields between package instances","Fail fast: any init error must skip the persist step entirely"],"tags":["chaincode","fabric","lifecycle","initialization"],"backgroundTag":"uninitialized-object","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"}