{"record":{"id":"c87c7175665e5b4d","repo":"apache/cassandra","slug":"cannot-read-value-of-length-d-only-d-bytes-rema","errorCode":null,"errorMessage":"Cannot read value of length %d, only %d bytes remaining in the message","messagePattern":"Cannot read value of length (.+?), only (.+?) bytes remaining in the message","errorType":"exception","errorClass":"org.apache.cassandra.transport.ProtocolException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/transport/CBUtil.java","lineNumber":476,"sourceCode":"        return ByteBuffer.wrap(readRawBytes(cb, length));\n    }\n\n    public static byte[] readValueAsBytes(ByteBuf cb)\n    {\n        int length = cb.readInt();\n        if (length < 0)\n            return null;\n\n        return readRawBytes(cb, length);\n    }\n\n    public static ByteBuffer readValueNoCopy(ByteBuf cb)\n    {\n        int length = cb.readInt();\n        if (length < 0)\n            return null;\n        if (length > cb.readableBytes())\n            throw new ProtocolException(String.format(\"Cannot read value of length %d, only %d bytes remaining in the message\",\n                                                      length, cb.readableBytes()));\n\n        ByteBuffer buffer = cb.nioBuffer(cb.readerIndex(), length);\n        cb.skipBytes(length);\n        return buffer;\n    }\n\n    public static ByteBuffer readBoundValue(ByteBuf cb, ProtocolVersion protocolVersion)\n    {\n        int length = cb.readInt();\n        if (length < 0)\n        {\n            if (protocolVersion.isSmallerThan(ProtocolVersion.V4)) // backward compatibility for pre-version 4\n                return null;\n            if (length == -1)\n                return null;\n            else if (length == -2)\n                return ByteBufferUtil.UNSET_BYTE_BUFFER;","sourceCodeStart":458,"sourceCodeEnd":494,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/transport/CBUtil.java#L458-L494","documentation":"CBUtil.readValueNoCopy reads a 4-byte length then, if non-negative, that many bytes as a read-only ByteBuffer view. If the declared length exceeds the remaining readable bytes, it throws ProtocolException reporting both the requested length and the bytes actually remaining. Guards value/[value] fields in CQL messages against truncation.","triggerScenarios":"A value field in a QUERY/EXECUTE/BATCH message declares a length greater than the remaining bytes of the frame — truncated body or malformed length prefix.","commonSituations":"Oversized bound values cut off by intermediaries; driver bug writing wrong lengths; parsing a frame of the wrong protocol version.","solutions":["Reconnect to resynchronize the protocol stream","Check intermediaries for frame truncation of large values (compressed frames especially)","Verify the driver protocol version matches the server","If building custom frames, ensure the 4-byte length equals the actual payload size"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { ... } catch (ProtocolException e) { if (e.getMessage().contains(\"bytes remaining in the message\")) { reconnectSession(); } else throw e; }","preventionTips":["Raise native_transport_max_frame_size for large bound values","Ensure proxies don't truncate large frames","Match compression and protocol versions across endpoints"],"tags":["cql-protocol","truncated-frame","values"],"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"}