{"record":{"id":"1530128118f2bd8f","repo":"apache/cassandra","slug":"not-enough-bytes-to-read-a-byte-array-preceded-by","errorCode":null,"errorMessage":"Not enough bytes to read a byte array preceded by its 2 bytes length","messagePattern":"Not enough bytes to read a byte array preceded by its 2 bytes length","errorType":"exception","errorClass":"org.apache.cassandra.transport.ProtocolException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/transport/CBUtil.java","lineNumber":241,"sourceCode":"    }\n\n    public static int sizeOfLongString(String str)\n    {\n        return 4 + encodedUTF8Length(str);\n    }\n\n    public static byte[] readBytes(ByteBuf cb)\n    {\n        try\n        {\n            int length = cb.readUnsignedShort();\n            byte[] bytes = new byte[length];\n            cb.readBytes(bytes);\n            return bytes;\n        }\n        catch (IndexOutOfBoundsException e)\n        {\n            throw new ProtocolException(\"Not enough bytes to read a byte array preceded by its 2 bytes length\");\n        }\n    }\n\n    public static void writeBytes(byte[] bytes, ByteBuf cb)\n    {\n        cb.writeShort(bytes.length);\n        cb.writeBytes(bytes);\n    }\n\n    public static int sizeOfBytes(byte[] bytes)\n    {\n        return 2 + bytes.length;\n    }\n\n    public static Map<String, ByteBuffer> readBytesMap(ByteBuf cb)\n    {\n        int length = cb.readUnsignedShort();\n        Map<String, ByteBuffer> m = new HashMap<>(length);","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/transport/CBUtil.java#L223-L259","documentation":"CBUtil.readBytes(ByteBuf) reads a 2-byte length followed by that many raw bytes. If the buffer lacks the declared bytes (IndexOutOfBoundsException), it throws ProtocolException noting the byte-array framing is incomplete. Protects binary field parsing in the CQL native protocol.","triggerScenarios":"A bytes field in a protocol message (e.g., protocol options, custom payload entries) declares a short length exceeding remaining readable bytes.","commonSituations":"Corrupted or truncated connection stream; misaligned parsing after an earlier bad read; buggy custom client writing wrong lengths for binary fields.","solutions":["Reconnect the client to reset the stream","Check for intermediaries that truncate or rewrite frames","Verify custom client code writes the correct 2-byte length before each byte array","Capture and inspect frames to find where the stream desyncs"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { ... } catch (ProtocolException e) { if (e.getMessage().contains(\"byte array preceded by its 2 bytes length\")) { reconnectSession(); } else throw e; }","preventionTips":["Write correct 2-byte length prefixes in custom clients","Reconnect after any protocol error rather than continuing on the stream","Verify compression settings on both ends"],"tags":["cql-protocol","truncated-frame","framing"],"backgroundTag":"unexpected-response-shape","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"}