{"record":{"id":"4d6b7584efaed9e5","repo":"aeron-io/aeron","slug":"offset-capacity-length","errorCode":null,"errorMessage":"offset= capacity= length=","messagePattern":"offset= capacity= length=","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"aeron-client/src/main/java/io/aeron/DirectBufferVector.java","lineNumber":153,"sourceCode":"    /**\n     * Ensure the vector is valid for the buffer.\n     *\n     * @throws NullPointerException if the buffer is null.\n     * @throws IllegalArgumentException if the offset is out of range for the buffer.\n     * @throws IllegalArgumentException if the length is out of range for the buffer.\n     * @return this for a fluent API.\n     */\n    public DirectBufferVector validate()\n    {\n        final int capacity = buffer.capacity();\n        if (offset < 0 || offset >= capacity)\n        {\n            throw new IllegalArgumentException(\"offset=\" + offset + \" capacity=\" + capacity);\n        }\n\n        if (length < 0 || length > (capacity - offset))\n        {\n            throw new IllegalArgumentException(\"offset=\" + offset + \" capacity=\" + capacity + \" length=\" + length);\n        }\n\n        return this;\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public String toString()\n    {\n        return \"DirectBufferVector{\" +\n            \"buffer=\" + buffer +\n            \", offset=\" + offset +\n            \", length=\" + length +\n            '}';\n    }\n","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-client/src/main/java/io/aeron/DirectBufferVector.java#L135-L171","documentation":"DirectBufferVector.validate() throws IllegalArgumentException when the vector's length is negative or length exceeds capacity - offset, i.e. the vector would read past the end of the underlying buffer. Aeron throws this before performing a vectored write to guarantee every vector stays inside its buffer. The message includes offset, capacity and length.","triggerScenarios":"Calling a vectored publish/offer API with a DirectBufferVector whose length < 0, or whose offset + length exceeds buffer.capacity(). Common when length is computed from a header field (e.g. frame or message length read from data) rather than validated against the buffer size.","commonSituations":"Trusting a length field parsed from a message or network frame that is corrupt or larger than the buffer; off-by-one in offset/length arithmetic; forgetting that length is relative to the vector's own offset, not buffer start.","solutions":["Validate length before constructing the vector: require 0 <= length && length <= buffer.capacity() - offset.","Clamp parsed/external length values to the remaining buffer space instead of using them verbatim.","Recheck whether the offset you pass already includes a base offset, causing offset+length to overshoot capacity."],"exampleFix":"// before\nint length = messageHeader.frameLength();\nDirectBufferVector v = new DirectBufferVector(buffer, offset, length);\n// after\nint length = Math.min(messageHeader.frameLength(), buffer.capacity() - offset);\nif (length < 0) { length = 0; }\nDirectBufferVector v = new DirectBufferVector(buffer, offset, length);","handlingStrategy":"validation","validationCode":"if (length < 0 || offset > buffer.capacity() - length) {\n    throw new IllegalArgumentException(\"length \" + length + \" exceeds capacity \" + buffer.capacity() + \" at offset \" + offset);\n}","typeGuard":"boolean fitsInBuffer(MutableDirectBuffer buf, int offset, int length) {\n    return buf != null && length >= 0 && offset >= 0 && length <= buf.capacity() - offset;\n}","tryCatchPattern":"try {\n    publication.offer(vectors);\n} catch (IllegalArgumentException e) {\n    log.error(\"vector length exceeds buffer bounds\", e);\n    dropBatch();\n}","preventionTips":["Clamp any externally-supplied or parsed length to buffer.capacity() - offset before use.","Remember length is relative to the vector offset, not the buffer start.","Unit-test vector construction against boundary conditions (offset 0, offset == capacity, length == capacity - offset)."],"tags":["aeron","buffer-vector","argument-validation","length-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"6d60124e15e35c11b49ba2e3c2c2858a09a18803","analyzedAt":"2026-09-12T11:17:07.683Z","contentChangedAt":"2026-09-12T11:17:07.683Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}