apache/flink · error · IndexOutOfBoundsException
Length cannot be negative.
Error message
Length cannot be negative.
What it means
Thrown by DataInputDeserializer.read(byte[], int off, int len) when len is negative. The method rejects negative read lengths up front with a clear message.
Source
Thrown at flink-core/src/main/java/org/apache/flink/core/memory/DataInputDeserializer.java:389
@Override
public void skipBytesToRead(int numBytes) throws IOException {
int skippedBytes = skipBytes(numBytes);
if (skippedBytes < numBytes) {
throw new EOFException("Could not skip " + numBytes + " bytes.");
}
}
@Override
public int read(@Nonnull byte[] b, int off, int len) throws IOException {
if (off < 0) {
throw new IndexOutOfBoundsException("Offset cannot be negative.");
}
if (len < 0) {
throw new IndexOutOfBoundsException("Length cannot be negative.");
}
if (b.length - off < len) {
throw new IndexOutOfBoundsException(
"Byte array does not provide enough space to store requested data" + ".");
}
if (this.position >= this.end) {
return len == 0 ? 0 : -1;
} else {
int toRead = Math.min(this.end - this.position, len);
System.arraycopy(this.buffer, this.position, b, off, toRead);
this.position += toRead;
return toRead;
}
}
View on GitHub (pinned to 2f3c205e92)
Solutions
- Ensure len >= 0 before calling read(b, off, len).
- Treat len == 0 explicitly (read returns 0) rather than passing a computed negative.
- Clamp len from external/sourced values: len = Math.max(0, requested).
Example fix
// before
input.read(dst, off, remaining); // remaining may be < 0
// after
if (remaining <= 0) {
return; // nothing to read
}
input.read(dst, off, remaining); Defensive patterns
Strategy: validation
Validate before calling
if (len < 0) {
throw new IndexOutOfBoundsException("len must be >= 0, got " + len);
}
input.read(dst, off, len); Prevention
- Clamp externally sourced lengths: len = Math.max(0, requested).
- Handle len == 0 explicitly rather than passing a computed negative.
- Treat a negative computed length as a bug in upstream size arithmetic.
When it happens
Trigger: Calling read(b, off, len) with len < 0.
Common situations: len derived from a size that underflowed to negative; a remaining-bytes computation that goes below zero; passing a -1 default sentinel.
Related errors
- Invalid bounds.
- Length may not be negative.
- Offset cannot be negative.
- Byte array does not provide enough space to store requested
- Could not skip {numBytes} bytes.
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/f474109c73cecd21.
Report an issue: GitHub.