NationalSecurityAgency/ghidra · error · IllegalArgumentException

Too many constants in {} to encode as a byte

Error message

Too many constants in {} to encode as a byte

What it means

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.

Source

Thrown at Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStoreFactory.java:673

		protected void doLoad(OT obj, DBRecord record)
				throws IllegalArgumentException, IllegalAccessException {
			setValue(obj, decode(record.getBinaryData(column)));
		}
	}

	/**
	 * The built-in codec for {@link Enum}
	 */
	public static class EnumDBByteFieldCodec<OT extends DBAnnotatedObject, E extends Enum<E>>
			extends AbstractDBFieldCodec<E, OT, ByteField> {
		private final E[] consts;

		@SuppressWarnings("unchecked")
		public EnumDBByteFieldCodec(Class<OT> objectType, Field field, int column) {
			super((Class<E>) field.getType(), objectType, ByteField.class, field, column);
			this.consts = valueType.getEnumConstants();
			if (consts.length > 255) {
				throw new IllegalArgumentException(
					"Too many constants in " + valueType + " to encode as a byte");
			}
		}

		@Override
		protected void doStore(OT obj, DBRecord record)
				throws IllegalArgumentException, IllegalAccessException {
			E value = getValue(obj);
			if (value == null) {
				record.setByteValue(column, (byte) -1);
			}
			else {
				record.setByteValue(column, (byte) value.ordinal());
			}
		}

		@Override
		public void store(E value, ByteField f) {

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Switch the field to a wider enum codec (e.g. a short/int-backed EnumDBFieldCodec variant) that can address the full ordinal range.
  2. If feasible, reduce the enum's constant count below 256 (merge/eliminate unused constants).
  3. Add a startup assertion on the enum size when the byte codec is selected, so the failure surfaces with a clearer message.

Example fix

// before
@DBAnnotatedField(codec = EnumDBByteFieldCodec.class)
E status; // E has >255 constants -> IllegalArgumentException

// after: use a codec wide enough for the ordinal range
@DBAnnotatedField(codec = EnumDBIntFieldCodec.class)
E status;
Defensive patterns

Strategy: validation

Validate before calling

// Before selecting the byte enum codec, check the enum size
if (enumType.getEnumConstants().length > 255) {
    // too large for byte codec: select a short/int-backed enum codec
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/caa07d5eb44ce9c8. Report an issue: GitHub.