data
data_error
ai_generated
true
Protobuf deserialization silently converts unknown enum values to 0 causing logic errors
ID: data/protobuf-enum-value-out-of-range
85%Fix Rate
86%Confidence
1Evidence
2023-07-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Protobuf 3.21.0+ | active | — | — | — |
| gRPC 1.50.0+ | active | — | — | — |
| Java Protobuf 3.21.0+ | active | — | — | — |
| Python protobuf 4.21.0+ | active | — | — | — |
Root Cause
Protobuf specification mandates that unknown enum values are preserved in the wire format but deserialized as 0 (the default enum value), leading to silent data corruption when the enum is used in business logic.
generic中文
Protobuf 规范要求未知枚举值在导线格式中保留,但反序列化为 0(默认枚举值),导致在业务逻辑中使用时静默数据损坏。
Official Documentation
https://protobuf.dev/programming-guides/enum/#unknownsWorkarounds
-
90% success Use `google.protobuf.EnumValueDescriptor` to check if the value is known: `if not enum_descriptor.values_by_number.get(raw_value): raise ValueError('Unknown enum')`
Use `google.protobuf.EnumValueDescriptor` to check if the value is known: `if not enum_descriptor.values_by_number.get(raw_value): raise ValueError('Unknown enum')` -
85% success Store the raw integer value in a separate field alongside the enum: `message Event { Status status = 1; int32 raw_status = 2; }`
Store the raw integer value in a separate field alongside the enum: `message Event { Status status = 1; int32 raw_status = 2; }` -
80% success Use a custom deserialization function that validates enum values against a known set before conversion.
Use a custom deserialization function that validates enum values against a known set before conversion.
中文步骤
使用 `google.protobuf.EnumValueDescriptor` 检查值是否已知:`if not enum_descriptor.values_by_number.get(raw_value): raise ValueError('Unknown enum')`将原始整数值存储在枚举旁边的单独字段中:`message Event { Status status = 1; int32 raw_status = 2; }`使用自定义反序列化函数,在转换之前根据已知集合验证枚举值。
Dead Ends
Common approaches that don't work:
-
70% fail
Protobuf treats 0 as the default even if named; unknown values still become 0, and logic cannot distinguish between 'intentional 0' and 'unknown'.
-
90% fail
`allow_alias` is for multiple names mapping to same number, not for handling unknown values.