go data_error ai_generated true

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

proto: cannot parse invalid wire-format data

ID: go/protobuf-unmarshal-invalid-wire

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

版本兼容性

版本状态引入弃用备注
google.golang.org/protobuf 1.x active — — —

根因分析

传给 proto.Unmarshal 的字节不符合 protobuf wire format——通常因为数据是 JSON、空、被截断,或完全是另一种 proto 消息类型。

English

The bytes passed to proto.Unmarshal do not conform to protobuf wire format — often because the data is JSON, empty, truncated, or a different proto message type entirely.

generic

解决方案

  1. 85% 成功率
    var msg pb.User
    if json.Valid(data) {
      err = protojson.Unmarshal(data, &msg)
    } else {
      err = proto.Unmarshal(data, &msg)
    }
    if err != nil { return fmt.Errorf("decode: %w", err) }
  2. 80% 成功率
    if len(data) > 0 {
      log.Printf("first bytes: %x", data[:min(len(data),16)])
    }
    // JSON starts with '{' = 0x7b; protobuf field tag usually 0x0a etc.

无效尝试

常见但无效的做法:

  1. 95% 失败

    Wire format is deterministic; identical bytes yield identical failure.

  2. 70% 失败

    If bytes are binary protobuf, JSON parser fails; if JSON, the original was wrong type.