apache/cassandra · error · IllegalArgumentException

invalid size for output buffer

Error message

invalid size for output buffer: ${outputLength}

What it means

IllegalArgumentException from ByteBufferUtil.ensureCapacity when the requested outputLength is invalid (e.g. negative). The guard fires before the resize-or-reallocate logic, since no buffer can satisfy a nonsensical target size; ${outputLength} is interpolated with the offending value.

Solutions

  1. Check the length computation for overflow/underflow (use long or Math.addExact)
  2. Validate the size read from serialized input is non-negative before use
  3. Treat corrupt-input cases as protocol/IO errors upstream; log the computed length
  4. Fix the caller that computed the negative value

Example fix

// before
int out = headerSize - consumed; // can go negative on corrupt data
buf = ByteBufferUtil.ensureCapacity(buf, out, true, bufferType);
// after
int out = headerSize - consumed;
if (out < 0) throw new CorruptBlockException("negative output size");
buf = ByteBufferUtil.ensureCapacity(buf, out, true, bufferType);
Defensive patterns

Strategy: validation

Validate before calling

// validate computed size before ensureCapacity
if (outputLength < 0)
    throw new IllegalArgumentException("caller computed negative size: " + outputLength + ", check for overflow/corrupt input");

Try / catch

try {
    buf = ByteBufferUtil.ensureCapacity(buf, outputLength, allowResize, bufferType);
} catch (IllegalArgumentException e) {
    // log computed length and input source; treat as corrupt input
}

Prevention

When it happens

Trigger: Calling ensureCapacity with a computed outputLength that underflowed (e.g., from a corrupted header, negative arithmetic, an int overflow, or a malformed serialized size read from a peer).

Common situations: Corrupt SSTable/network data yielding negative sizes; integer overflow in size computation; passing an unvalidated length parsed from untrusted bytes.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/afdd402c1023a46d. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/utils/ByteBufferUtil.java:914

    {
        BufferType bufferType = buf != null ? BufferType.typeOf(buf) : BufferType.ON_HEAP;
        return ensureCapacity(buf, outputLength, allowBufferResize, bufferType);
    }

    /**
     * Ensure {@code buf} is large enough for {@code outputLength}. If not, it is cleaned up and a new buffer is allocated;
     * else; buffer has it's position/limit set appropriately.
     *
     * @param buf buffer to test the size of; may be null, in which case, a new buffer is allocated.
     * @param outputLength the minimum target size of the buffer
     * @param allowBufferResize true if resizing (reallocating) the buffer is allowed
     * @param bufferType on- or off- heap byte buffer
     * @return {@code buf} if it was large enough, else a newly allocated buffer.
     */
    public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize, BufferType bufferType)
    {
        if (0 > outputLength)
            throw new IllegalArgumentException("invalid size for output buffer: " + outputLength);
        if (buf == null || buf.capacity() < outputLength)
        {
            if (!allowBufferResize)
                throw new IllegalStateException(String.format("output buffer is not large enough for data: current capacity %d, required %d", buf.capacity(), outputLength));
            MemoryUtil.clean(buf);
            buf = bufferType.allocate(outputLength);
        }
        else
        {
            buf.position(0).limit(outputLength);
        }
        return buf;
    }

    /**
     * Check is the given buffer contains a given sub-buffer.
     *
     * @param buffer The buffer to search for sequence of bytes in.

View on GitHub (pinned to 88fd0f6a0e)