apache/hadoop · error · IndexOutOfBoundsException
len({}) < 0.
Error message
len({}) < 0. What it means
Second guard in CBZip2OutputStream.write(byte[], offs, len): a negative length throws IndexOutOfBoundsException("len(...) < 0."). A negative length can never denote a valid byte range, so it is rejected immediately instead of being treated as zero or looping backwards through the buffer.
Source
Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/bzip2/CBZip2OutputStream.java:866
bsFinishedWithStream();
}
/**
* Returns the blocksize parameter specified at construction time.
* @return blocksize.
*/
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 (this.out == null) {
throw new IOException("stream closed");
}
for (int hi = offs + len; offs < hi;) {
write0(buf[offs++]);
}
}
private void write0(int b) throws IOException {
if (this.currentChar != -1) {
b &= 0xff;
if (this.currentChar == b) {View on GitHub (pinned to 2add963021)
Solutions
- Check the length before calling: if (len < 0) throw new IllegalArgumentException("bad len " + len);
- Clamp semantics intentionally: treat 'nothing to write' as len == 0 (skip the call) instead of passing a negative remaining count.
- Separate 'error' return values (-1) from valid lengths at the API boundary; never forward them.
- Prefer Objects.checkFromToIndex(offs, offs + len, buf.length) for a single consistent bounds check.
Example fix
// before
int remaining = total - written; // can go negative on short reads
cbz.write(buf, off, remaining);
// after
int remaining = Math.max(0, total - written);
if (remaining > 0) {
cbz.write(buf, off, remaining);
} Defensive patterns
Strategy: validation
Validate before calling
if (len < 0) {
throw new IllegalArgumentException("len=" + len + " for write()");
}
cbz.write(buf, offs, len); Prevention
- Clamp 'remaining' computations with Math.max(0, ...) before writing.
- Separate error codes (-1) from valid lengths at API boundaries.
- Keep (buf, off, len) parameter order consistent across your wrappers.
When it happens
Trigger: write(buf, offs, len) with len < 0: remaining-bytes computations like (limit - pos) going negative after the cursor passed the limit, error returns (-1) from size/parse functions used as lengths, or swapped offs/len arguments putting a negative value in len.
Common situations: Read loops that compute remaining = total - written without checking sign when the stream ends early; APIs returning -1 for 'unknown/none' being forwarded as a length; argument-order mixups after refactors between (len, offs) styles.
Related errors
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/aae95ba88096d3eb.
Report an issue: GitHub.