apache/hadoop · error · IndexOutOfBoundsException

offs({}) < 0.

Error message

offs({}) < 0.

What it means

CBZip2OutputStream.write(byte[], offs, len) validates its arguments before doing work. A negative offset yields IndexOutOfBoundsException("offs(...) < 0."). This mirrors the standard OutputStream contract: offsets before the array start are a caller bug and are rejected up front rather than reading out of bounds.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/bzip2/CBZip2OutputStream.java:863

    bsPutUByte(0x90);

    bsPutInt(this.combinedCRC);
    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 {

View on GitHub (pinned to 2add963021)

Solutions

  1. Validate/normalize the offset before the call: if (offs < 0) throw new IllegalArgumentException(...) with the offending value.
  2. Use java.util.Objects.checkFromToIndex(offs, offs + len, buf.length) once for all three bounds.
  3. Fix the sentinel handling: never pass indexOf() results unchecked; map -1 to a skip/default path.
  4. Add unit tests for boundary offsets (0, len, buf.length) on every OutputStream wrapper.

Example fix

// before
cbz.write(buf, offset, len); // offset == -1 from indexOf()

// after
int offset = src.indexOf(marker);
if (offset < 0) {
  return; // marker absent, nothing to write
}
cbz.write(buf, offset, len);
Defensive patterns

Strategy: validation

Validate before calling

Objects.checkFromToIndex(offs, offs + len, buf.length); // throws before the codec does
cbz.write(buf, offs, len);

Prevention

When it happens

Trigger: write(buf, offs, len) with offs < 0: loop variables decremented past zero, a parsed offset like '-1' used as 'not found' sentinel, or arithmetic such as (pos - headerSize) going negative for small inputs.

Common situations: Sentinel -1 from indexOf/lastIndexOf flowing into an offset parameter; parsers computing offsets from user-supplied numbers without validation; refactors that swapped parameter order (offs/len) making a small length land in offs.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/42fd3fdea24190fa. Report an issue: GitHub.