apache/cassandra · error · InvalidTypeException

Invalid 32-bits integer value, expecting 4 bytes but got %d

Error message

Invalid 32-bits integer value, expecting 4 bytes but got %d

What it means

Thrown by TypeCodec.IntCodec.deserializeNoBoxing(ByteBuffer, ProtocolVersion) when the incoming byte buffer does not contain exactly 4 bytes, which is the fixed serialized width of a CQL int. This indicates the wire payload is truncated or is not actually an int value (e.g. a different type's bytes were decoded as int). deserializeNoBoxing returns 0 only for null/empty buffers; any other wrong length is fatal.

Source

Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:1707

        {
            if (value == null) return "NULL";
            return Integer.toString(value);
        }

        @Override
        public ByteBuffer serializeNoBoxing(int value, ProtocolVersion protocolVersion)
        {
            ByteBuffer bb = ByteBuffer.allocate(4);
            bb.putInt(0, value);
            return bb;
        }

        @Override
        public int deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion)
        {
            if (bytes == null || bytes.remaining() == 0) return 0;
            if (bytes.remaining() != 4)
                throw new InvalidTypeException(
                "Invalid 32-bits integer value, expecting 4 bytes but got " + bytes.remaining());

            return bytes.getInt(bytes.position());
        }
    }

    /**
     * This codec maps a CQL {@link DataType#timestamp()} to a Java {@link Date}.
     */
    private static class TimestampCodec extends TypeCodec<Date>
    {

        private static final TimestampCodec instance = new TimestampCodec();

        private TimestampCodec()
        {
            super(DataType.timestamp(), Date.class);
        }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Verify the CQL column type actually is int and the codec used matches (IntCodec only for DataType.cint()).
  2. Check buffer construction: ensure all 4 bytes were written and position/limit are correct before deserializing.
  3. Inspect any custom codecs and protocol-version handling; align with the server's ProtocolVersion.
  4. Catch InvalidTypeException, log bytes.remaining() and the raw bytes, and fix the producer of the buffer.

Example fix

// before
int v = TypeCodec.cint().deserialize(bb, protocolVersion);
// after
if (bb == null || bb.remaining() == 0) { v = 0; }
else if (bb.remaining() != 4) { throw new IllegalStateException("expected 4 bytes for cint, got " + bb.remaining()); }
else { v = bb.getInt(bb.position()); }
Defensive patterns

Strategy: validation

Validate before calling

static boolean isIntBuffer(java.nio.ByteBuffer bb) {
    return bb != null && (bb.remaining() == 0 || bb.remaining() == 4);
}

Try / catch

try {
    int v = TypeCodec.cint().deserializeNoBoxing(bb, protocolVersion);
} catch (InvalidTypeException e) {
    log.error("cint decode failed ({} bytes)", bb == null ? -1 : bb.remaining(), e);
    throw new SerializationException(e);
}

Prevention

When it happens

Trigger: Calling deserializeNoBoxing (or TypeCodec.deserialize for cint) with a ByteBuffer whose remaining() != 4 — e.g. decoding a serialized varint, smallint, or a partial buffer; hand-rolling protocol framing and slicing the value short.

Common situations: Custom codec registration mistakes (decoding with IntCodec when the column is another type); protocol/paging bugs truncating buffers; migrating from other drivers/serialization formats with different fixed widths; version mismatch between client protocol framing and server.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/da385def45f2d4c3. Report an issue: GitHub.