apache/flink · error · IndexOutOfBoundsException

offset: %d, length: %d, size: %d

Error message

offset: %d, length: %d, size: %d

What it means

Thrown by DataOutputSerializer.write(MemorySegment segment, int off, int len) when the offset/length into the MemorySegment are out of bounds: len<0, off<0, or off > segment.size()-len. The message reports the offending offset, length, and segment size for diagnosis.

Source

Thrown at flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java:127

        write(b, 0, b.length);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        if (len < 0 || off > b.length - len) {
            throw new ArrayIndexOutOfBoundsException();
        }
        if (this.position > this.buffer.length - len) {
            resize(len);
        }
        System.arraycopy(b, off, this.buffer, this.position, len);
        this.position += len;
    }

    @Override
    public void write(MemorySegment segment, int off, int len) throws IOException {
        if (len < 0 || off < 0 || off > segment.size() - len) {
            throw new IndexOutOfBoundsException(
                    String.format("offset: %d, length: %d, size: %d", off, len, segment.size()));
        }
        if (this.position > this.buffer.length - len) {
            resize(len);
        }
        segment.get(off, this.buffer, this.position, len);
        this.position += len;
    }

    @Override
    public void writeBoolean(boolean v) throws IOException {
        write(v ? 1 : 0);
    }

    @Override
    public void writeByte(int v) throws IOException {
        write(v);
    }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Validate off>=0, len>=0, and off+len<=segment.size() before writing.
  2. Use segment.size() to clamp the read region.
  3. Log segment.size() alongside off/len when the precondition fails to catch off-by-one region math.

Example fix

// before
out.write(segment, regionOffset, regionLen);

// after
if (regionOffset < 0 || regionLen < 0 || regionOffset + regionLen > segment.size()) {
    throw new IndexOutOfBoundsException("bad segment region");
}
out.write(segment, regionOffset, regionLen);
Defensive patterns

Strategy: validation

Validate before calling

if (off < 0 || len < 0 || off + len > segment.size()) {
    throw new IndexOutOfBoundsException(
        String.format("bad segment region: off=%d len=%d size=%d", off, len, segment.size()));
}
out.write(segment, off, len);

Prevention

When it happens

Trigger: Calling write(segment, off, len) with off<0, len<0, or off+len exceeding segment.size().

Common situations: Incorrect segment offset arithmetic when copying a region; reading past the end of a MemorySegment; a length derived from a record size that exceeds the segment.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/0ea178a1d77bd6cf. Report an issue: GitHub.