go runtime_error ai_generated true

panic:proto:使用 nil 调用 Marshal

panic: proto: Marshal called with nil

ID: go/protobuf-nil-message-marshal

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-12-02首次发现

版本兼容性

版本状态引入弃用备注
google.golang.org/protobuf v1.28+ active

根因分析

使用 nil 消息指针调用 proto.Marshal,通常是因为某函数返回了 (nil, nil) 或结构体字段未初始化。

English

proto.Marshal was called with a nil message pointer, typically after a function returned (nil, nil) or an uninitialized struct field.

generic

解决方案

  1. 92% 成功率
    Guard against nil before marshaling:
    if msg == nil { return errors.New("nil message") }
    b, err := proto.Marshal(msg)
  2. 90% 成功率
    Fix the producer function to never return (nil, nil):
    func build() (*pb.Msg, error) {
      if bad { return nil, errBad }
      return &pb.Msg{...}, nil
    }

无效尝试

常见但无效的做法:

  1. 80% 失败

    Hides the bug; the caller still receives garbage or a swallowed error.

  2. 70% 失败

    Masks the real problem (nil constructor) and may send semantically empty data.