{
  "id": "data/protobuf-enum-value-out-of-range",
  "signature": "Protobuf deserialization silently converts unknown enum values to 0 causing logic errors",
  "signature_zh": "Protobuf 反序列化静默地将未知枚举值转换为 0，导致逻辑错误",
  "regex": "unknown enum value.*converted to 0|enum.*out of range|protobuf.*enum.*default",
  "domain": "data",
  "category": "data_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "Protobuf 规范要求未知枚举值在导线格式中保留，但反序列化为 0（默认枚举值），导致在业务逻辑中使用时静默数据损坏。",
  "versions": [
    {
      "version": "Protobuf 3.21.0+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "gRPC 1.50.0+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java Protobuf 3.21.0+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Python protobuf 4.21.0+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Protobuf treats 0 as the default even if named; unknown values still become 0, and logic cannot distinguish between 'intentional 0' and 'unknown'.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "`allow_alias` is for multiple names mapping to same number, not for handling unknown values.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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')`",
      "success_rate": 0.9,
      "how": "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')`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Store the raw integer value in a separate field alongside the enum: `message Event { Status status = 1; int32 raw_status = 2; }`",
      "success_rate": 0.85,
      "how": "Store the raw integer value in a separate field alongside the enum: `message Event { Status status = 1; int32 raw_status = 2; }`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a custom deserialization function that validates enum values against a known set before conversion.",
      "success_rate": 0.8,
      "how": "Use a custom deserialization function that validates enum values against a known set before conversion.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "使用 `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; }`",
    "使用自定义反序列化函数，在转换之前根据已知集合验证枚举值。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://protobuf.dev/programming-guides/enum/#unknowns",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-07-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}