{"record":{"id":"22a00be9e8baef83","repo":"hyperledger/fabric","slug":"error-marshaling-proto-marshal-called-with-nil-22a00b","errorCode":null,"errorMessage":"error marshaling: proto: Marshal called with nil","messagePattern":"error marshaling: proto: Marshal called with nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"protoutil/txutils.go","lineNumber":98,"sourceCode":"\tdataMsg proto.Message,\n\tmsgVersion int32,\n\tepoch uint64,\n\ttlsCertHash []byte,\n) (*common.Envelope, error) {\n\tpayloadChannelHeader := MakeChannelHeader(txType, msgVersion, channelID, epoch)\n\tpayloadChannelHeader.TlsCertHash = tlsCertHash\n\tvar err error\n\tpayloadSignatureHeader := &common.SignatureHeader{}\n\n\tif signer != nil {\n\t\tpayloadSignatureHeader, err = NewSignatureHeader(signer)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\tif !dataMsg.ProtoReflect().IsValid() {\n\t\treturn nil, errors.New(\"error marshaling: proto: Marshal called with nil\")\n\t}\n\tdata, err := proto.Marshal(dataMsg)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"error marshaling\")\n\t}\n\n\tpaylBytes := MarshalOrPanic(\n\t\t&common.Payload{\n\t\t\tHeader: MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader),\n\t\t\tData:   data,\n\t\t},\n\t)\n\n\tvar sig []byte\n\tif signer != nil {\n\t\tsig, err = signer.Sign(paylBytes)\n\t\tif err != nil {\n\t\t\treturn nil, err","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/protoutil/txutils.go#L80-L116","documentation":"CreateSignedEnvelopeWithTLSBinding explicitly rejects a dataMsg message that is a typed-nil or otherwise invalid protobuf message before calling proto.Marshal, returning 'error marshaling: proto: Marshal called with nil'. Passing a nil concrete message (e.g. (*common.Payload)(nil)) to proto.Marshal panics or errors, so the library guards and returns this sentinel error instead.","triggerScenarios":"Calling CreateSignedEnvelopeWithTLSBinding (or the envelope helpers built on it: SeekInfoBlocksFrom, SeekInfoHeadersFrom, seekHelper, seekLastEnvelope, seekNextEnvelope, createDeliverEnvelope) with a nil typed message, e.g. nil *common.Payload, nil *SeekInfo, or nil *Envelope.","commonSituations":"A variable holding a typed-nil pointer because an earlier constructor returned (nil, err) and the err wasn't checked; Go interface-holding-typed-nil pitfalls when passing messages through interface{} parameters.","solutions":["Check that the message passed to CreateSignedEnvelopeWithTLSBinding is non-nil before calling it","Audit the code path that produced the message for an unchecked (nil, err) return from a constructor or unmarshal call","Avoid typed-nil interface pitfalls: return untyped nil on error paths, or check with reflection/is-nil helper","If the message can legitimately be absent, skip envelope creation instead of marshaling"],"exampleFix":"// before\nenv, _ := protoutil.CreateSignedEnvelopeWithTLSBinding(\n    common.HeaderType_ENDORSER_TRANSACTION, chID, creator, payload, nil, 0)\n// after\nif payload == nil {\n    return nil, fmt.Errorf(\"payload is nil\")\n}\nenv, err := protoutil.CreateSignedEnvelopeWithTLSBinding(\n    common.HeaderType_ENDORSER_TRANSACTION, chID, creator, payload, nil, 0)\nif err != nil {\n    return nil, err\n}","handlingStrategy":"validation","validationCode":"func marshalGuard(msg proto.Message) error {\n    if msg == nil || !msg.ProtoReflect().IsValid() {\n        return fmt.Errorf(\"message is nil or invalid\")\n    }\n    return nil\n}","typeGuard":"func isNilProtoMsg(m interface{}) bool {\n    if m == nil { return true }\n    v := reflect.ValueOf(m)\n    switch v.Kind() {\n    case reflect.Ptr, reflect.Interface:\n        return v.IsNil()\n    }\n    return false\n}","tryCatchPattern":"if err := marshalGuard(dataMsg); err != nil {\n    return nil, err\n}\nenv, err := protoutil.CreateSignedEnvelopeWithTLSBinding(t, chID, signer, dataMsg, tlsCertHash, 0)\nif err != nil {\n    return nil, fmt.Errorf(\"createSignedEnvelope: %w\", err)\n}","preventionTips":["Always check the (msg, err) pair from constructors before passing msg on","Return untyped nil on error paths to avoid typed-nil interface traps","Run nil checks in helper wrappers around envelope construction"],"tags":["protobuf","marshal","nil-pointer","hyperledger-fabric"],"backgroundTag":"proto-marshal-nil-message","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"}