go
runtime_error
ai_generated
true
panic: proto: 消息为 nil
panic: proto: message is nil
ID: go/protobuf-nil-pointer-unmarshal
80%修复率
89%置信度
0证据数
2024-06-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.x | active | — | — | — |
根因分析
proto.Unmarshal 或 proto.Marshal 被传入了 nil 消息指针,例如 var m *pb.User; proto.Unmarshal(data, m)。生成的方法解引用该指针并 panic。
English
proto.Unmarshal or proto.Marshal was called with a nil message pointer, e.g. var m *pb.User; proto.Unmarshal(data, m). The generated methods dereference the pointer and panic.
解决方案
-
95% 成功率
Always allocate with m := &pb.User{} or m := new(pb.User) before Unmarshal: proto.Unmarshal(data, m). -
88% 成功率
Use proto.Clone or a constructor helper that guarantees a non-nil pointer, and add a staticcheck/errcheck rule to catch nil pointer args.
无效尝试
常见但无效的做法:
-
80% 失败
Recovering hides a programming bug and returns a zero-value message that downstream logic treats as valid data.
-
85% 失败
The panic occurs on the call itself, before any error is returned, so the check never runs.