data
serialization_error
ai_generated
true
Protobuf deserialization produces wrong values when field numbers are reused across different message types
ID: data/protobuf-field-number-collision
90%Fix Rate
84%Confidence
1Evidence
2023-09-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Protocol Buffers 3.21.0 | active | — | — | — |
| protoc 24.0 | active | — | — | — |
| gRPC 1.57.0 | active | — | — | — |
Root Cause
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.
generic中文
Protobuf线格式仅使用字段编号,而非名称,因此在同一文件中跨不同消息类型重复使用字段编号可能导致反序列化期间字段值被分配给错误的字段。
Official Documentation
https://protobuf.dev/programming-guides/encoding/#fieldsWorkarounds
-
95% success 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
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 -
85% success 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
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
中文步骤
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 numbersUse 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
Dead Ends
Common approaches that don't work:
-
Adding deprecated annotations to conflicting fields
80% fail
Adding fields with new numbers does not fix the existing field number collision issue.
-
Using 'reserved' keyword for conflicting field numbers
75% fail
Reserved prevents new use but does not fix existing serialized data with wrong field numbers.