{"record":{"id":"caa07d5eb44ce9c8","repo":"NationalSecurityAgency/ghidra","slug":"too-many-constants-in-to-encode-as-a-byte","errorCode":null,"errorMessage":"Too many constants in {} to encode as a byte","messagePattern":"Too many constants in (.+?) to encode as a byte","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStoreFactory.java","lineNumber":673,"sourceCode":"\t\tprotected void doLoad(OT obj, DBRecord record)\n\t\t\t\tthrows IllegalArgumentException, IllegalAccessException {\n\t\t\tsetValue(obj, decode(record.getBinaryData(column)));\n\t\t}\n\t}\n\n\t/**\n\t * The built-in codec for {@link Enum}\n\t */\n\tpublic static class EnumDBByteFieldCodec<OT extends DBAnnotatedObject, E extends Enum<E>>\n\t\t\textends AbstractDBFieldCodec<E, OT, ByteField> {\n\t\tprivate final E[] consts;\n\n\t\t@SuppressWarnings(\"unchecked\")\n\t\tpublic EnumDBByteFieldCodec(Class<OT> objectType, Field field, int column) {\n\t\t\tsuper((Class<E>) field.getType(), objectType, ByteField.class, field, column);\n\t\t\tthis.consts = valueType.getEnumConstants();\n\t\t\tif (consts.length > 255) {\n\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Too many constants in \" + valueType + \" to encode as a byte\");\n\t\t\t}\n\t\t}\n\n\t\t@Override\n\t\tprotected void doStore(OT obj, DBRecord record)\n\t\t\t\tthrows IllegalArgumentException, IllegalAccessException {\n\t\t\tE value = getValue(obj);\n\t\t\tif (value == null) {\n\t\t\t\trecord.setByteValue(column, (byte) -1);\n\t\t\t}\n\t\t\telse {\n\t\t\t\trecord.setByteValue(column, (byte) value.ordinal());\n\t\t\t}\n\t\t}\n\n\t\t@Override\n\t\tpublic void store(E value, ByteField f) {","sourceCodeStart":655,"sourceCodeEnd":691,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStoreFactory.java#L655-L691","documentation":"Thrown (unchecked IllegalArgumentException) by EnumDBByteFieldCodec's constructor when the enum type has more than 255 constants. This codec stores the enum ordinal as a single byte (with -1 reserved for null), so it can only address ordinals 0..254; larger enums cannot be byte-encoded and must use a wider codec.","triggerScenarios":"Declaring a DB-annotated enum field of type EnumDBByteFieldCodec on an enum class that declares 256 or more constants. The byte-storage codec is selected automatically/by convention for enum fields, but it cannot fit the ordinal range.","commonSituations":"A large lookup-table-style enum (e.g. opcode tables, status-code enums) exceeding 255 values; an enum that grew during development past the byte limit; code generation that always picks the byte codec for enums.","solutions":["Switch the field to a wider enum codec (e.g. a short/int-backed EnumDBFieldCodec variant) that can address the full ordinal range.","If feasible, reduce the enum's constant count below 256 (merge/eliminate unused constants).","Add a startup assertion on the enum size when the byte codec is selected, so the failure surfaces with a clearer message."],"exampleFix":"// before\n@DBAnnotatedField(codec = EnumDBByteFieldCodec.class)\nE status; // E has >255 constants -> IllegalArgumentException\n\n// after: use a codec wide enough for the ordinal range\n@DBAnnotatedField(codec = EnumDBIntFieldCodec.class)\nE status;","handlingStrategy":"validation","validationCode":"// Before selecting the byte enum codec, check the enum size\nif (enumType.getEnumConstants().length > 255) {\n    // too large for byte codec: select a short/int-backed enum codec\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use a short/int-backed enum codec for enums with > 255 constants.","Add a startup assertion on enum size when the byte codec is auto-selected.","Reduce the enum's constant count below 256 if possible."],"tags":["database","codec","enum","byte-limit","illegal-argument","unchecked-exception"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}