alibaba/Sentinel · error · IllegalArgumentException

maxSize must > 0

Error message

maxSize must > 0

What it means

SimpleHttpResponseParser reads an entire HTTP response (status line + headers + body) into a single pre-allocated byte buffer. Its constructor validates the buffer size and throws IllegalArgumentException("maxSize must > 0") for a negative argument. Note the off-by-one in the guard: it checks maxSize < 0, so 0 is accepted (creating an empty buffer) even though the message demands > 0 — a known inconsistency in this utility.

Source

Thrown at sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpResponseParser.java:44

 * <p>
 * The parser provides functionality to parse raw bytes HTTP response to a {@link SimpleHttpResponse}.
 * </p>
 * <p>
 * Note that this is a very NAIVE parser, {@code Content-Length} must be specified in the
 * HTTP response header, otherwise, the body will be dropped. All other body type such as
 * {@code Transfer-Encoding: chunked}, {@code Transfer-Encoding: deflate} are not supported.
 * </p>
 *
 * @author leyou
 */
public class SimpleHttpResponseParser {

    private static final int MAX_BODY_SIZE = 1024 * 1024 * 4;
    private byte[] buf;

    public SimpleHttpResponseParser(int maxSize) {
        if (maxSize < 0) {
            throw new IllegalArgumentException("maxSize must > 0");
        }
        this.buf = new byte[maxSize];
    }

    public SimpleHttpResponseParser() {
        this(1024 * 4);
    }

    /**
     * Parse bytes from an input stream to a {@link SimpleHttpResponse}.
     *
     * @param in input stream
     * @return parsed HTTP response entity
     * @throws IOException when an IO error occurs
     */
    public SimpleHttpResponse parse(InputStream in) throws IOException {
        int bg = 0;
        int len;

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Pass a positive buffer size (default constructor uses 1024 * 4)
  2. Clamp computed sizes: Math.max(defaultSize, computedSize)
  3. Passing 0 'works' but is wrong — it creates an empty buffer; treat 0 as invalid at the call site too

Example fix

// before
int size = cfg.getInt("parser.buf", -1); // -1 sentinel
parser = new SimpleHttpResponseParser(size);

// after
int size = cfg.getInt("parser.buf", 1024 * 4);
parser = new SimpleHttpResponseParser(Math.max(1, size));
Defensive patterns

Strategy: validation

Validate before calling

int size = Math.max(1, configuredParserBufferSize);
parser = new SimpleHttpResponseParser(size);

Prevention

When it happens

Trigger: new SimpleHttpResponseParser(-1) or any negative maxSize; in practice a computed buffer size (e.g. from a config value or max-body arithmetic) that goes negative. Passing 0 does not throw but will immediately fail later with the buf-index IndexOutOfBoundsException.

Common situations: Custom heartbeat/HTTP client code parameterizing the parser with a configurable read buffer; subtraction-based size math (headerSize - overhead) that can go negative; tests passing sentinel values like -1 for "default".

Related errors


AI-assisted analysis of alibaba/Sentinel@a3f40ba8e9 (2026-08-14). Data as JSON: /api/errors/010139140dc5359b. Report an issue: GitHub.