apache/shenyu · warning · IllegalArgumentException

Max response body size must not be negative

Error message

Max response body size must not be negative

What it means

readLimitedResponseBody enforces that the caller-supplied maxBodySize limit is non-negative; a negative limit throws IllegalArgumentException('Max response body size must not be negative') before any reading occurs.

Solutions

  1. Pass a non-negative maxBodySize (e.g. 0, or a real byte limit)
  2. If 'unlimited' is intended, choose a large positive constant or use a different read method that has no cap
  3. Validate/normalize the configured limit at load time (Math.max(0, configured)) before calling

Example fix

// before
long max = config.getMaxBodySize(); // -1 = unlimited
String body = HttpUtils.readLimitedResponseBody(rb, max);
// after
long max = Math.max(0, config.getMaxBodySize());
String body = HttpUtils.readLimitedResponseBody(rb, max == UNLIMITED ? Long.MAX_VALUE : max);
Defensive patterns

Strategy: validation

Validate before calling

if (maxBodySize < 0) throw new ConfigurationException("maxBodySize must be >= 0");

Type guard

boolean isValidLimit(long max) { return max >= 0; }

Try / catch

try { body = HttpUtils.readLimitedResponseBody(rb, max); } catch (IllegalArgumentException e) { if (e.getMessage().contains("must not be negative")) { max = DEFAULT_MAX; body = HttpUtils.readLimitedResponseBody(rb, max); } }

Prevention

When it happens

Trigger: Calling HttpUtils.readLimitedResponseBody(body, -1) or passing an uninitialized/computed negative limit — e.g. a config value of -1 used as 'unlimited' by the caller, which this API does not accept.

Common situations: Configuring max body size via properties where -1 means unlimited elsewhere; arithmetic producing a negative limit; copy-paste from APIs where -1 is the convention for no limit.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/shenyu@567142e072 (2026-09-12). Data as JSON: /api/errors/5780b47edd9ea58e. Report an issue: GitHub.

Appendix: source

Thrown at shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java:480

            return this.name();
        }
    }

    /**
     * Read response body with a size limit to prevent excessive memory usage.
     *
     * @param responseBody the response body to read
     * @param maxBodySize  maximum allowed body size in bytes
     * @return the response body as a string
     * @throws IOException              if an I/O error occurs
     * @throws IllegalArgumentException if the body exceeds maxBodySize
     */
    public static String readLimitedResponseBody(final ResponseBody responseBody, final long maxBodySize) throws IOException {
        if (Objects.isNull(responseBody)) {
            throw new IllegalArgumentException("Response body is empty");
        }
        if (maxBodySize < 0) {
            throw new IllegalArgumentException("Max response body size must not be negative");
        }

        long contentLength = responseBody.contentLength();
        if (contentLength > maxBodySize) {
            throw new IllegalArgumentException(String.format(
                    "Response body exceeds maximum size of %d bytes", maxBodySize));
        }

        ByteArrayOutputStream outputStream = contentLength > 0
                ? new ByteArrayOutputStream((int) Math.min(contentLength, Integer.MAX_VALUE))
                : new ByteArrayOutputStream();
        byte[] buffer = new byte[READ_BUFFER_SIZE];
        long totalBytes = 0;
        try (InputStream inputStream = responseBody.byteStream()) {
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                totalBytes += bytesRead;
                if (totalBytes > maxBodySize) {

View on GitHub (pinned to 567142e072)