go runtime_error ai_generated true

panic:运行时错误:无效的内存地址或空指针解引用

panic: runtime error: invalid memory address or nil pointer dereference

ID: go/proto-nil-message-panic

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

版本兼容性

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

根因分析

向 proto.Marshal、proto.Clone 传递了 nil *Message 指针,或在 nil 接收者上调用了会解引用 map 字段的生成 getter。

English

A nil *Message pointer was passed to proto.Marshal, proto.Clone, or a generated getter was called on a nil receiver that dereferences a map field.

generic

解决方案

  1. 95% 成功率
    if msg == nil {
        return nil, status.Error(codes.InvalidArgument, "nil message")
    }
    data, err := proto.Marshal(msg)
    if err != nil { return nil, err }
  2. 85% 成功率
    clone := proto.Clone(msg) // returns nil if msg is nil
    if clone == nil {
        return nil, errors.New("nil proto message")
    }

无效尝试

常见但无效的做法:

  1. 85% 失败

    Masks the nil bug; the same nil pointer will surface elsewhere or cause silent data loss.

  2. 95% 失败

    AllowPartial only affects required-field validation, not nil pointer dereferences.