{"record":{"id":"c662a01bdff55c61","repo":"apache/cassandra","slug":"output-buffer-is-not-large-enough-for-data-curren","errorCode":null,"errorMessage":"output buffer is not large enough for data: current capacity %d, required %d","messagePattern":"output buffer is not large enough for data: current capacity (.+?), required (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/ByteBufferUtil.java","lineNumber":918,"sourceCode":"\n    /**\n     * Ensure {@code buf} is large enough for {@code outputLength}. If not, it is cleaned up and a new buffer is allocated;\n     * else; buffer has it's position/limit set appropriately.\n     *\n     * @param buf buffer to test the size of; may be null, in which case, a new buffer is allocated.\n     * @param outputLength the minimum target size of the buffer\n     * @param allowBufferResize true if resizing (reallocating) the buffer is allowed\n     * @param bufferType on- or off- heap byte buffer\n     * @return {@code buf} if it was large enough, else a newly allocated buffer.\n     */\n    public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize, BufferType bufferType)\n    {\n        if (0 > outputLength)\n            throw new IllegalArgumentException(\"invalid size for output buffer: \" + outputLength);\n        if (buf == null || buf.capacity() < outputLength)\n        {\n            if (!allowBufferResize)\n                throw new IllegalStateException(String.format(\"output buffer is not large enough for data: current capacity %d, required %d\", buf.capacity(), outputLength));\n            MemoryUtil.clean(buf);\n            buf = bufferType.allocate(outputLength);\n        }\n        else\n        {\n            buf.position(0).limit(outputLength);\n        }\n        return buf;\n    }\n\n    /**\n     * Check is the given buffer contains a given sub-buffer.\n     *\n     * @param buffer The buffer to search for sequence of bytes in.\n     * @param subBuffer The buffer to match.\n     *\n     * @return true if buffer contains sub-buffer, false otherwise.\n     */","sourceCodeStart":900,"sourceCodeEnd":936,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/ByteBufferUtil.java#L900-L936","documentation":"IllegalArgumentException from ByteBufferUtil.ensureCapacity when the existing buffer's capacity is below the required outputLength and buffer resizing is not allowed (allowBufferResize false). The caller demanded in-place capacity that the buffer cannot provide, so the operation fails rather than leaking/replacing the buffer.","triggerScenarios":"Calling ensureCapacity (via compression/checksum paths such as ICompressor) with a caller-owned buffer too small for the output and resizing explicitly disallowed.","commonSituations":"Pre-allocated buffer sized from a stale/incorrect max-chunk estimate; buffer reused across differing compression ratios; off-heap buffer too small for worst-case compression expansion.","solutions":["Set allowBufferResize=true if the caller permits reallocation","Pre-size the buffer to the maximum possible output (e.g., compressor.maxCompressedLength(input))","Use input size plus a safety margin for worst-case compression expansion","Pool buffers keyed by size class so capacity always matches"],"exampleFix":"// before\nbuf = ByteBufferUtil.ensureCapacity(buf, needed, false, BufferType.OFF_HEAP);\n// after\nint max = compressor.maxCompressedLength(inputLength);\nif (buf == null || buf.capacity() < max)\n    buf = BufferType.OFF_HEAP.allocate(max);\nbuf = ByteBufferUtil.ensureCapacity(buf, needed, true, BufferType.OFF_HEAP);","handlingStrategy":"try-catch","validationCode":"// pre-size buffer to worst case\nint required = compressor.maxCompressedLength(inputLength);\nif (buf == null || buf.capacity() < required)\n    buf = bufferType.allocate(required);","typeGuard":null,"tryCatchPattern":"try {\n    buf = ByteBufferUtil.ensureCapacity(buf, outputLength, false, bufferType);\n} catch (IllegalStateException e) {\n    // parse 'required %d' from message and reallocate\n}","preventionTips":["Size buffers from compressor max-output formulas, not heuristics","Prefer allowBufferResize=true unless profiling forbids allocation","Pool buffers by size class to keep capacities sufficient"],"tags":["bytebuffer","buffer-capacity","compression"],"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"}