No explicit error; silent data corruption
data
protocol_error
ai_generated
partial
Protobuf 反序列化静默丢弃未知的 oneof 字段导致数据丢失
Protobuf deserialization silently drops unknown oneof fields causing data loss
ID: data/protobuf-oneof-unknown-field
80%修复率
83%置信度
1证据数
2023-11-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Protocol Buffers 3.21.0 | active | — | — | — |
| gRPC 1.58.0 | active | — | — | — |
根因分析
当包含 oneof 字段的 Protobuf 消息被不知道新 oneof 变体的旧版本反序列化时,整个 oneof 被设置为 None,丢失该 oneof 中所有其他已知字段。
English
When a Protobuf message with a oneof field is deserialized by an older version that doesn't know the new oneof variant, the entire oneof is set to None, losing all other known fields in that oneof.
官方文档
https://protobuf.dev/programming-guides/proto3/#oneof解决方案
-
Add a catch-all 'bytes unknown_oneof' field to the message to preserve raw bytes of unknown oneof variants, then manually parse.
-
Ensure all consumers are updated to the latest proto definition before deploying new oneof variants.
无效尝试
常见但无效的做法:
-
Adding a new field outside the oneof to carry the new variant
50% 失败
This changes the message structure and may cause confusion, and the oneof still gets cleared on unknown variants.
-
Using 'optional' keyword for oneof fields
70% 失败
The 'optional' keyword does not prevent the oneof from being cleared when an unknown variant is encountered.