{"record":{"id":"6389be594df0bf18","repo":"hyperledger/fabric","slug":"block-data-is-nil-6389be","errorCode":null,"errorMessage":"block data is nil","messagePattern":"block data is nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"protoutil/commonutils.go","lineNumber":97,"sourceCode":"\terr = errors.Wrapf(err, \"error unmarshalling message for type %s\", headerType)\n\treturn chdr, err\n}\n\n// ExtractEnvelopeOrPanic retrieves the requested envelope from a given block\n// and unmarshals it -- it panics if either of these operations fail\nfunc ExtractEnvelopeOrPanic(block *cb.Block, index int) *cb.Envelope {\n\tenvelope, err := ExtractEnvelope(block, index)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn envelope\n}\n\n// ExtractEnvelope retrieves the requested envelope from a given block and\n// unmarshals it\nfunc ExtractEnvelope(block *cb.Block, index int) (*cb.Envelope, error) {\n\tif block.Data == nil {\n\t\treturn nil, errors.New(\"block data is nil\")\n\t}\n\n\tenvelopeCount := len(block.Data.Data)\n\tif index < 0 || index >= envelopeCount {\n\t\treturn nil, errors.New(\"envelope index out of bounds\")\n\t}\n\tmarshaledEnvelope := block.Data.Data[index]\n\tenvelope, err := GetEnvelopeFromBlock(marshaledEnvelope)\n\terr = errors.WithMessagef(err, \"block data does not carry an envelope at index %d\", index)\n\treturn envelope, err\n}\n\n// MakeChannelHeader creates a ChannelHeader.\nfunc MakeChannelHeader(headerType cb.HeaderType, version int32, chainID string, epoch uint64) *cb.ChannelHeader {\n\ttm := timestamppb.Now()\n\ttm.Nanos = 0\n\treturn &cb.ChannelHeader{\n\t\tType:      int32(headerType),","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/protoutil/commonutils.go#L79-L115","documentation":"ExtractEnvelope returns this error when the supplied *cb.Block has a nil Data field, i.e. the block carries no transaction data section at all. A well-formed Fabric block always has a non-nil Data (possibly empty), so a nil Data indicates an uninitialized or malformed block struct.","triggerScenarios":"Passing a *cb.Block created as &cb.Block{} or decoded from an empty/truncated byte slice to ExtractEnvelope, before the len() bounds check can run.","commonSituations":"Hand-constructed blocks in tests, blocks deserialized from a corrupted file ledger, APIs that return a bare Block without data on failure, block protos partially populated by external tooling.","solutions":["Check block.Data for nil (and len(block.Data.Data)) before calling ExtractEnvelope, or use IsConfigBlock-style nil checks.","Trace where the block was produced/decoded — fix the source to always populate Data (e.g. use protoutil.NewBlock).","If read from disk, validate the ledger storage; a truncated serialization can yield a Data-less block.","For genesis-like empty blocks, special-case index access instead of calling ExtractEnvelope."],"exampleFix":"// before\nenv, err := protoutil.ExtractEnvelope(block, 0) // 'block data is nil' on empty struct\n// after\nif block.Data == nil || len(block.Data.Data) == 0 {\n    return fmt.Errorf(\"block %d has no data\", block.Header.Number)\n}\nenv, err := protoutil.ExtractEnvelope(block, 0)","handlingStrategy":"validation","validationCode":"if block == nil || block.Data == nil {\n    return errors.New(\"refusing to extract: block has nil Data\")\n}\nenv, err := protoutil.ExtractEnvelope(block, index)","typeGuard":"func blockHasData(block *cb.Block) bool {\n    return block != nil && block.Data != nil\n}","tryCatchPattern":"env, err := protoutil.ExtractEnvelope(block, 0)\nif err != nil {\n    if err.Error() == \"block data is nil\" {\n        return fmt.Errorf(\"block %d is malformed (no data section)\", block.GetHeader().GetNumber())\n    }\n    return err\n}","preventionTips":["Never construct *cb.Block by hand; use protoutil.NewBlock so Data is initialized","Validate blocks decoded from disk/network before use","Treat a nil Data block as a storage-integrity signal and re-fetch from the ordering service","Check IsConfigBlock-style nil guards as a habit before block accessors"],"tags":["hyperledger-fabric","nil-check","block-processing"],"backgroundTag":"nil-value","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"}