{"record":{"id":"0ec8257d476f0bf0","repo":"apache/cassandra","slug":"invalid-16-bits-integer-value-expecting-2-bytes-b","errorCode":null,"errorMessage":"Invalid 16-bits integer value, expecting 2 bytes but got %d","messagePattern":"Invalid 16-bits integer value, expecting 2 bytes but got (.+?)","errorType":"exception","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":1645,"sourceCode":"        {\n            if (value == null) return \"NULL\";\n            return Short.toString(value);\n        }\n\n        @Override\n        public ByteBuffer serializeNoBoxing(short value, ProtocolVersion protocolVersion)\n        {\n            ByteBuffer bb = ByteBuffer.allocate(2);\n            bb.putShort(0, value);\n            return bb;\n        }\n\n        @Override\n        public short deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion)\n        {\n            if (bytes == null || bytes.remaining() == 0) return 0;\n            if (bytes.remaining() != 2)\n                throw new InvalidTypeException(\n                \"Invalid 16-bits integer value, expecting 2 bytes but got \" + bytes.remaining());\n\n            return bytes.getShort(bytes.position());\n        }\n    }\n\n    /**\n     * This codec maps a CQL {@link DataType#cint()} to a Java {@link Integer}.\n     */\n    private static class IntCodec extends PrimitiveIntCodec\n    {\n\n        private static final IntCodec instance = new IntCodec();\n\n        private IntCodec()\n        {\n            super(DataType.cint());\n        }","sourceCodeStart":1627,"sourceCodeEnd":1663,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L1627-L1663","documentation":"Thrown by ShortCodec.deserializeNoBoxing when the ByteBuffer does not contain exactly 2 bytes, the wire size of a CQL smallint. Null/empty buffers return 0; any other length cannot be read as a 16-bit signed integer.","triggerScenarios":"Calling ShortCodec.deserializeNoBoxing(bytes, protocolVersion) with a ByteBuffer whose remaining() is 1, 4, or 8, typically when a tinyint/int/bigint payload is deserialized as smallint.","commonSituations":"Environment schema drift (column types altered in one cluster only), manual parsing of UDT or collection elements with the wrong codec, or replaying binary logs/legacy data with mixed widths.","solutions":["Confirm the column's CQL type (smallint = 2 bytes) and select the matching codec.","Log bytes.remaining() on failure to identify the true encoded width and reroute to ByteCodec/IntCodec/LongCodec as appropriate.","Prefer typed row getters (getShort/getInt) over manual codec invocation so the codec matches the declared type.","Align schema between producer and consumer clusters, or add a migration for the column."],"exampleFix":"// before\nshort s = new ShortCodec().deserializeNoBoxing(bytes, ProtocolVersion.V4);\n// after\nint n = bytes.remaining();\nif (n == 2)\n    short s = new ShortCodec().deserializeNoBoxing(bytes, ProtocolVersion.V4);\nelse if (n == 4)\n    int i = new IntCodec().deserializeNoBoxing(bytes, ProtocolVersion.V4);\nelse\n    throw new IllegalArgumentException(\"Unexpected smallint width: \" + n);","handlingStrategy":"validation","validationCode":"if (bytes == null || bytes.remaining() == 0) return (short) 0;\nif (bytes.remaining() != 2)\n    throw new IllegalArgumentException(\"Expected 2 bytes for smallint, got \" + bytes.remaining());","typeGuard":"boolean isShortSized(ByteBuffer b) { return b != null && (b.remaining() == 0 || b.remaining() == 2); }","tryCatchPattern":"try { return codec.deserializeNoBoxing(bytes, protocolVersion); }\ncatch (InvalidTypeException e) {\n    log.warn(\"Smallint deserialization failed: {} bytes\", bytes.remaining());\n    return (short) 0;\n}","preventionTips":["Select codec by column DataType, not assumption.","Use row.getShort/getInt instead of manual deserialization.","Keep schemas in sync across clusters.","Log buffer length on deserialization failures."],"tags":["serialization","smallint","bytebuffer","deserialization"],"backgroundTag":"type-mismatch","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"}