# proto: Marshal called with nil message

- **ID:** `go/protobuf-proto-marshal-nil-message`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A nil protobuf message pointer was passed to proto.Marshal or protojson.Marshal, typically from a handler that returned a nil pointer on error paths without checking.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| google.golang.org/protobuf 1.28+ | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   if msg == nil {
    return nil, status.Error(codes.Internal, "nil message")
}
b, err := proto.Marshal(msg)
if err != nil { return nil, status.Error(codes.Internal, err.Error()) }
   ```
2. **** (90% success)
   ```
   func (s *srv) Get(ctx context.Context, req *pb.GetReq) (*pb.GetResp, error) {
    if req == nil { return &pb.GetResp{}, nil }
    return &pb.GetResp{Id: req.Id}, nil
}
   ```

## Dead Ends

- **** — Swallowing the panic leaves the caller with no message and no error, causing silent data loss downstream. (85% fail)
- **** — Changes wire semantics; clients expecting a specific message type get an unrelated type and may fail to unmarshal. (70% fail)
