{"record":{"id":"ba207de1dfb9c9d1","repo":"elastic/elasticsearch","slug":"bufferlimit-must-be-greater-than-0","errorCode":null,"errorMessage":"bufferLimit must be greater than 0","messagePattern":"bufferLimit must be greater than 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java","lineNumber":55,"sourceCode":"\n/**\n * Default implementation of {@link org.apache.http.nio.protocol.HttpAsyncResponseConsumer}. Buffers the whole\n * response content in heap memory, meaning that the size of the buffer is equal to the content-length of the response.\n * Limits the size of responses that can be read based on a configurable argument. Throws an exception in case the entity is longer\n * than the configured buffer limit.\n */\npublic class HeapBufferedAsyncResponseConsumer extends AbstractAsyncResponseConsumer<HttpResponse> {\n\n    private final int bufferLimitBytes;\n    private volatile HttpResponse response;\n    private volatile SimpleInputBuffer buf;\n\n    /**\n     * Creates a new instance of this consumer with the provided buffer limit\n     */\n    public HeapBufferedAsyncResponseConsumer(int bufferLimit) {\n        if (bufferLimit <= 0) {\n            throw new IllegalArgumentException(\"bufferLimit must be greater than 0\");\n        }\n        this.bufferLimitBytes = bufferLimit;\n    }\n\n    /**\n     * Get the limit of the buffer.\n     */\n    public int getBufferLimit() {\n        return bufferLimitBytes;\n    }\n\n    @Override\n    protected void onResponseReceived(HttpResponse httpResponse) throws HttpException, IOException {\n        this.response = httpResponse;\n    }\n\n    @Override\n    protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException {","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java#L37-L73","documentation":"HeapBufferedAsyncResponseConsumer buffers the response body on the heap up to a fixed byte limit. The constructor rejects bufferLimit <= 0 because a zero/negative buffer cannot hold any entity, making the consumer useless; the message is a hard precondition with no recovery inside the constructor.","triggerScenarios":"Instantiating HeapBufferedAsyncResponseConsumer(0) or with a negative value, usually via a misconfigured RestClientBuilder.setMaxRetryTimeoutMillis / setHttpAsyncResponseConsumerFactory that derives the limit from a config that resolved to 0.","commonSituations":"A config property for response buffer size is unset and defaults to 0; arithmetic that produces <= 0; passing a byte count parsed from a malformed string.","solutions":["Pass a positive byte limit (e.g. a few MB) when constructing the consumer or its factory.","Validate/derive the limit from configuration with Math.max(1, ...) and fix the config default.","Prefer using the RestClientBuilder helper which supplies a sane default consumer factory."],"exampleFix":"// before\nnew HeapBufferedAsyncResponseConsumer(configuredLimit); // configuredLimit=0\n// after\nnew HeapBufferedAsyncResponseConsumer(Math.max(1, configuredLimit));","handlingStrategy":"validation","validationCode":"int safeLimit = Math.max(1, configuredBufferLimit);\nnew HeapBufferedAsyncResponseConsumer(safeLimit);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate buffer-limit config at load time.","Use the RestClientBuilder default consumer factory unless you have specific tuning needs.","Document the unit (bytes) clearly in your config to avoid 0-from-unit-confusion."],"tags":["rest-client","validation","memory","preconditions"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}