{"record":{"id":"705645fdf12d6d1b","repo":"apache/cassandra","slug":"length-d-max-length-d","errorCode":null,"errorMessage":"Length %d > max length %d","messagePattern":"Length (.+?) > max length (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/ByteBufferUtil.java","lineNumber":865,"sourceCode":"\n    // Doesn't change bb position\n    public static int getShortLength(ByteBuffer bb, int position)\n    {\n        return getUnsignedShort(bb, position);\n    }\n\n    // changes bb position\n    public static int readShortLength(ByteBuffer bb)\n    {\n        int length = (bb.get() & 0xFF) << 8;\n        return length | (bb.get() & 0xFF);\n    }\n\n    // changes bb position\n    public static void writeShortLength(ByteBuffer bb, int length)\n    {\n        if (length > FBUtilities.MAX_UNSIGNED_SHORT)\n            throw new IllegalArgumentException(String.format(\"Length %d > max length %d\", length, FBUtilities.MAX_UNSIGNED_SHORT));\n        bb.put((byte) ((length >> 8) & 0xFF));\n        bb.put((byte) (length & 0xFF));\n    }\n\n    // changes bb position\n    public static ByteBuffer readBytes(ByteBuffer bb, int length)\n    {\n        ByteBuffer copy = bb.duplicate();\n        copy.limit(copy.position() + length);\n        bb.position(bb.position() + length);\n        return copy;\n    }\n\n    // changes bb position\n    public static ByteBuffer readBytesWithShortLength(ByteBuffer bb)\n    {\n        int length = readShortLength(bb);\n        return readBytes(bb, length);","sourceCodeStart":847,"sourceCodeEnd":883,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/ByteBufferUtil.java#L847-L883","documentation":"writeShortLength serializes a length as an unsigned 16-bit short. If the length exceeds FBUtilities.MAX_UNSIGNED_SHORT (65535), the value cannot be represented and IllegalArgumentException is thrown before any bytes are written.","triggerScenarios":"Calling writeShortLength (directly or via short-length serialization paths) with a byte payload longer than 65535 bytes.","commonSituations":"Serializing oversized blobs with a short-length format; misusing the short-length helpers for data that requires the int-length (vint/unsigned) variants.","solutions":["Use the int-length / vint-length variant for payloads > 65535 bytes","Split or chunk data so each piece fits in 65535 bytes","Validate payload size before calling and fail early with a clearer message","Check which serialization format expects short lengths and switch formats"],"exampleFix":"// before\nByteBufferUtil.writeShortLength(bb, blob.remaining()); // fails if > 65535\n// after\nif (blob.remaining() > FBUtilities.MAX_UNSIGNED_SHORT) {\n    ByteBufferUtil.writeWithVIntLength(bb, blob.remaining());\n} else {\n    ByteBufferUtil.writeShortLength(bb, blob.remaining());\n}","handlingStrategy":"validation","validationCode":"// guard before short-length serialization\nif (data.remaining() > FBUtilities.MAX_UNSIGNED_SHORT)\n    throw new IllegalArgumentException(\"payload too large for short-length format: \" + data.remaining());","typeGuard":null,"tryCatchPattern":"try {\n    ByteBufferUtil.writeShortLength(bb, len);\n} catch (IllegalArgumentException e) {\n    // switch to int/vint-length serialization\n}","preventionTips":["Never use short-length formats for user-supplied blob sizes","Add a size assertion in serialization helpers","Know the 65535 limit of unsigned short encoding"],"tags":["bytebuffer","serialization","value-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}