{"record":{"id":"da385def45f2d4c3","repo":"apache/cassandra","slug":"invalid-32-bits-integer-value-expecting-4-bytes-b","errorCode":null,"errorMessage":"Invalid 32-bits integer value, expecting 4 bytes but got %d","messagePattern":"Invalid 32-bits integer value, expecting 4 bytes but got (.+?)","errorType":"exception","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":1707,"sourceCode":"        {\n            if (value == null) return \"NULL\";\n            return Integer.toString(value);\n        }\n\n        @Override\n        public ByteBuffer serializeNoBoxing(int value, ProtocolVersion protocolVersion)\n        {\n            ByteBuffer bb = ByteBuffer.allocate(4);\n            bb.putInt(0, value);\n            return bb;\n        }\n\n        @Override\n        public int deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion)\n        {\n            if (bytes == null || bytes.remaining() == 0) return 0;\n            if (bytes.remaining() != 4)\n                throw new InvalidTypeException(\n                \"Invalid 32-bits integer value, expecting 4 bytes but got \" + bytes.remaining());\n\n            return bytes.getInt(bytes.position());\n        }\n    }\n\n    /**\n     * This codec maps a CQL {@link DataType#timestamp()} to a Java {@link Date}.\n     */\n    private static class TimestampCodec extends TypeCodec<Date>\n    {\n\n        private static final TimestampCodec instance = new TimestampCodec();\n\n        private TimestampCodec()\n        {\n            super(DataType.timestamp(), Date.class);\n        }","sourceCodeStart":1689,"sourceCodeEnd":1725,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L1689-L1725","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the CQL column type actually is int and the codec used matches (IntCodec only for DataType.cint()).","Check buffer construction: ensure all 4 bytes were written and position/limit are correct before deserializing.","Inspect any custom codecs and protocol-version handling; align with the server's ProtocolVersion.","Catch InvalidTypeException, log bytes.remaining() and the raw bytes, and fix the producer of the buffer."],"exampleFix":"// before\nint v = TypeCodec.cint().deserialize(bb, protocolVersion);\n// after\nif (bb == null || bb.remaining() == 0) { v = 0; }\nelse if (bb.remaining() != 4) { throw new IllegalStateException(\"expected 4 bytes for cint, got \" + bb.remaining()); }\nelse { v = bb.getInt(bb.position()); }","handlingStrategy":"validation","validationCode":"static boolean isIntBuffer(java.nio.ByteBuffer bb) {\n    return bb != null && (bb.remaining() == 0 || bb.remaining() == 4);\n}","typeGuard":null,"tryCatchPattern":"try {\n    int v = TypeCodec.cint().deserializeNoBoxing(bb, protocolVersion);\n} catch (InvalidTypeException e) {\n    log.error(\"cint decode failed ({} bytes)\", bb == null ? -1 : bb.remaining(), e);\n    throw new SerializationException(e);\n}","preventionTips":["Confirm the target column type is actually int before decoding with IntCodec.","Keep buffer position/limit correct: deserialize reads from position(), not index 0.","Register custom codecs per exact DataType to avoid cross-type decoding.","When hand-rolling protocol framing, assert fixed 4-byte width for cint values."],"tags":["cassandra","codec","serialization","bytebuffer","invalid-type-exception"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}