MuntashirAkon/AppManager · error · IndexOutOfBoundsException

len(" + len + ") < 0.

Error message

len(" + len + ") < 0.

What it means

BZip2CompressorOutputStream.write(byte[], int, int) validates its arguments before writing and throws IndexOutOfBoundsException when len is negative. This mirrors the java.io.OutputStream contract so callers detect bad buffer ranges early instead of corrupting the compressed stream.

Source

Thrown at app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java:610

        bsFinishedWithStream();
    }

    /**
     * Returns the blocksize parameter specified at construction time.
     * @return the blocksize parameter specified at construction time
     */
    public final int getBlockSize() {
        return this.blockSize100k;
    }

    @Override
    public void write(final byte[] buf, int offs, final int len)
            throws IOException {
        if (offs < 0) {
            throw new IndexOutOfBoundsException("offs(" + offs + ") < 0.");
        }
        if (len < 0) {
            throw new IndexOutOfBoundsException("len(" + len + ") < 0.");
        }
        if (offs + len > buf.length) {
            throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
                    + len + ") > buf.length("
                    + buf.length + ").");
        }
        if (closed) {
            throw new IOException("Stream closed");
        }

        for (final int hi = offs + len; offs < hi;) {
            write0(buf[offs++]);
        }
    }

    /**
     * Keeps track of the last bytes written and implicitly performs
     * run-length encoding as the first step of the bzip2 algorithm.

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Fix the caller's length computation so len is never negative before invoking write.
  2. Clamp or guard: only call write when len > 0 (skip the call when len == 0).
  3. If indices come from external input, validate 0 <= len <= buf.length - offs upstream and fail with a clear message.

Example fix

// before
out.write(buf, off, end - off);
// after
int len = end - off;
if (len > 0) {
    out.write(buf, off, len);
}
Defensive patterns

Strategy: validation

Validate before calling

if (len < 0 || offs < 0 || offs + len > buf.length) {
    throw new IllegalArgumentException("bad write range: offs=" + offs + " len=" + len);
}
out.write(buf, offs, len);

Try / catch

try {
    out.write(buf, offs, len);
} catch (IndexOutOfBoundsException e) {
    // log offs/len/buf.length and fix the caller's range math
}

Prevention

When it happens

Trigger: Calling write(buf, offs, len) with len < 0, typically from a computed length like end-start where end < start, or an int overflow producing a negative count.

Common situations: Wrapping code that slices buffers with off-by-one index math; passing a remaining-bytes counter that already went negative; copying from a buffer shorter than expected.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/1771b64cdaf9d762. Report an issue: GitHub.