go
runtime_error
ai_generated
true
panic: 运行时错误:proto.Unmarshal 中无效的内存地址或 nil 指针解引用
panic: runtime error: invalid memory address or nil pointer dereference in proto.Unmarshal
ID: go/proto-nil-pointer-deref-unmarshal
80%修复率
86%置信度
0证据数
2024-05-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| google.golang.org/protobuf v1.26+ | active | — | — | — |
根因分析
调用 proto.Unmarshal 时传入了 nil 消息指针,或生成消息的内部字段未初始化(例如使用零值结构体字面量而非构造函数)。
English
proto.Unmarshal was called with a nil message pointer, or a generated message's internal fields were not initialized (e.g., using a zero-value struct literal instead of a constructor).
解决方案
-
95% 成功率
msg := &pb.User{} if err := proto.Unmarshal(data, msg); err != nil { return err } -
90% 成功率
msg := pb.NewUser() // or &pb.User{} then reset if err := proto.Unmarshal(data, msg); err != nil { return err }
无效尝试
常见但无效的做法:
-
85% 失败
Swallowing the panic leaves the message nil; subsequent field access panics again and corrupts control flow.
-
80% 失败
proto.Unmarshal requires a proto.Message (pointer receiver); passing a value fails to compile or operates on a copy.