go data_error ai_generated true

proto: 无法解析无效的 wire-format 数据

proto: cannot parse invalid wire-format data

ID: go/proto-unmarshal-invalid-wire-type

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

版本兼容性

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

根因分析

传给 proto.Unmarshal 的字节不符合 protobuf wire 格式。通常是因为把 JSON、纯文本或截断/拼接的缓冲区交给了二进制 proto 反序列化器。

English

The bytes handed to proto.Unmarshal do not conform to the protobuf wire format. Usually caused by passing JSON, plain text, or a truncated/concatenated buffer to a binary proto unmarshaler.

generic

解决方案

  1. 90% 成功率
    if err := proto.Unmarshal(data, msg); err != nil {
        // if data is JSON, use:
        err = protojson.Unmarshal(data, msg)
    }
  2. 85% 成功率
    log.Printf("len=%d head=%x", len(data), data[:min(16,len(data))])

无效尝试

常见但无效的做法:

  1. 95% 失败

    The buffer is deterministic; malformed bytes never become valid on retry. The error is not transient.

  2. 90% 失败

    On wire-format errors the message is left in an undefined/partial state; downstream logic reads zero or garbage fields.