{"record":{"id":"e72364d61385bb59","repo":"apache/cassandra","slug":"not-enough-bytes-to-read-an-utf8-serialized-string","errorCode":null,"errorMessage":"Not enough bytes to read an UTF8 serialized string preceded by its 2 bytes length","messagePattern":"Not enough bytes to read an UTF8 serialized string 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":152,"sourceCode":"            cb.readerIndex(cb.readerIndex() + length);\n            return str;\n        }\n        catch (IllegalStateException | CharacterCodingException e)\n        {\n            throw new ProtocolException(\"Cannot decode string as UTF8: '\" + ByteBufferUtil.bytesToHex(buffer) + \"'; \" + e);\n        }\n    }\n\n    public static String readString(ByteBuf cb)\n    {\n        try\n        {\n            int length = cb.readUnsignedShort();\n            return readString(cb, length);\n        }\n        catch (IndexOutOfBoundsException e)\n        {\n            throw new ProtocolException(\"Not enough bytes to read an UTF8 serialized string preceded by its 2 bytes length\");\n        }\n    }\n\n    /**\n     * Write US-ASCII strings. It does not work if containing any char > 0x007F (127)\n     * @param str satisfies {@link org.apache.cassandra.db.marshal.AsciiType}\n     *             i.e. seven-bit ASCII, a.k.a. ISO646-US\n     */\n    public static void writeAsciiString(String str, ByteBuf cb)\n    {\n        cb.writeShort(str.length());\n        ByteBufUtil.writeAscii(cb, str);\n    }\n\n    public static void writeString(String str, ByteBuf cb)\n    {\n        int length = encodedUTF8Length(str);\n        Preconditions.checkArgument(length <= Short.MAX_VALUE,","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/transport/CBUtil.java#L134-L170","documentation":"CBUtil.readString(ByteBuf) reads a 2-byte unsigned short length then that many UTF-8 bytes. If the buffer has fewer bytes than the declared length (IndexOutOfBoundsException), it throws ProtocolException stating the short-string framing was incomplete. This guards against truncated or malformed protocol frames.","triggerScenarios":"A short string field in a CQL protocol message declares a length exceeding the remaining readable bytes — truncated frame, wrong protocol version parsing, or a corrupt message.","commonSituations":"Proxy or load balancer truncating frames; a client writing a wrong length prefix; parsing frames from a different protocol version than they were encoded with.","solutions":["Check network path for truncation (proxies, packet size limits) and retransmit","Confirm client and server use the same native protocol version","Reconnect the client session to resynchronize the stream","Capture frames and validate the 2-byte length against the actual payload"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { ... } catch (ProtocolException e) { if (e.getMessage().contains(\"Not enough bytes to read an UTF8 serialized string preceded by its 2 bytes length\")) { reconnectSession(); } else throw e; }","preventionTips":["Check proxies/LBs for frame truncation","Pin the protocol version explicitly in driver config","Monitor for connections that die right after handshake (sign of desync)"],"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"}