{"record":{"id":"83b06b8e44ff2dac","repo":"aeron-io/aeron","slug":"limit-outside-range-capacity-capacity-limit-limit","errorCode":null,"errorMessage":"limit outside range: capacity=<capacity> limit=<limit>","messagePattern":"limit outside range: capacity=<capacity> limit=<limit>","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"aeron-client/src/main/java/io/aeron/BufferBuilder.java","lineNumber":134,"sourceCode":"     * The current limit of the buffer that has been used by append operations.\n     *\n     * @return the current limit of the buffer that has been used by append operations.\n     */\n    public int limit()\n    {\n        return limit;\n    }\n\n    /**\n     * Set this limit for this buffer as the position at which the next append operation will occur.\n     *\n     * @param limit to be the new value.\n     */\n    public void limit(final int limit)\n    {\n        if (limit < 0 || limit >= buffer.capacity())\n        {\n            throw new IllegalArgumentException(\n                \"limit outside range: capacity=\" + buffer.capacity() + \" limit=\" + limit);\n        }\n\n        this.limit = limit;\n    }\n\n    /**\n     * Get the value which the next term offset for a fragment to be assembled should begin at.\n     *\n     * @return the value which the next term offset for a fragment to be assembled should begin at.\n     */\n    public int nextTermOffset()\n    {\n        return nextTermOffset;\n    }\n\n    /**\n     * Set the value which the next term offset for a fragment to be assembled should begin at.","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-client/src/main/java/io/aeron/BufferBuilder.java#L116-L152","documentation":"BufferBuilder.limit(int) requires 0 <= limit < buffer.capacity(); the current limit must always be a valid position within the allocated buffer. The message reports the buffer's current capacity and the rejected limit value.","triggerScenarios":"Calling builder.limit(n) with n < 0 or n >= buffer.capacity() (capacity changes as the builder resizes, so a previously valid limit can become invalid).","commonSituations":"Caching an old limit value across a resize/reset; setting limit to buffer capacity off-by-one (limit is exclusive); setting limit before the buffer has grown to the desired size.","solutions":["Set limit only to values in [0, capacity-1]; query buffer capacity if exposed, or use reset()/resize-appropriate APIs rather than raw limits.","Re-read the current limit/capacity after any operation that may resize the builder instead of caching values.","Fix off-by-one: remember limit is an exclusive end position, so max valid value is capacity - 1."],"exampleFix":"// before\nbuilder.limit(builder.capacity()); // off-by-one / stale\n// after\nint cap = builder.capacity();\nif (desiredLimit >= 0 && desiredLimit < cap) {\n    builder.limit(desiredLimit);\n}","handlingStrategy":"validation","validationCode":"if (newLimit >= 0 && newLimit < builder.capacity()) builder.limit(newLimit);","typeGuard":null,"tryCatchPattern":"try {\n    builder.limit(desired);\n} catch (IllegalArgumentException e) {\n    // stale/oversized limit after resize; recompute from current state\n}","preventionTips":["Never cache limit across reset/resize operations","Remember limit is exclusive: valid range is 0..capacity-1","Set limit after growing the buffer, not before"],"tags":["aeron","buffer","bounds","illegal-argument"],"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"}