data
serialization_error
ai_generated
true
当字段编号在不同消息类型之间重复使用时,Protobuf反序列化产生错误值
Protobuf deserialization produces wrong values when field numbers are reused across different message types
ID: data/protobuf-field-number-collision
90%修复率
84%置信度
1证据数
2023-09-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Protocol Buffers 3.21.0 | active | — | — | — |
| protoc 24.0 | active | — | — | — |
| gRPC 1.57.0 | active | — | — | — |
根因分析
Protobuf线格式仅使用字段编号,而非名称,因此在同一文件中跨不同消息类型重复使用字段编号可能导致反序列化期间字段值被分配给错误的字段。
English
Protobuf wire format uses field numbers only, not names, so reusing field numbers across different message types in the same file can cause field values to be assigned to the wrong fields during deserialization.
官方文档
https://protobuf.dev/programming-guides/encoding/#fields解决方案
-
Ensure all message types use unique field numbers across the entire .proto file: message User { int32 id = 1; } message Product { int32 product_id = 1; } // Change to use distinct numbers -
Use a migration script to rewrite existing serialized data with corrected field numbers: for each message, decode using old schema, then re-encode with new schema mapping old field numbers to new ones
无效尝试
常见但无效的做法:
-
Adding deprecated annotations to conflicting fields
80% 失败
Adding fields with new numbers does not fix the existing field number collision issue.
-
Using 'reserved' keyword for conflicting field numbers
75% 失败
Reserved prevents new use but does not fix existing serialized data with wrong field numbers.