apache/hadoop · error · EOFException

position is negative

Error message

position is negative

What it means

The positioned read read(position, buffer, offset, length) on ObjectMultiRangeInputStream validates arguments per the HDFS contract: position < 0 throws EOFException("position is negative"). Buffer/offset/length are validated separately by FSUtils.checkReadParameters, and reads at or beyond contentLength simply return -1. EOFException (not IndexOutOfBoundsException) is used deliberately to match HDFS behavior.

Source

Thrown at hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/object/ObjectMultiRangeInputStream.java:147

      if (n < 0) {
        return total == 0 ? -1 : total;
      }

      total += n;
      offset += n;
      currPos += n;
      nextPos += n;
    }

    return total;
  }

  @Override
  public int read(long position, byte[] buffer, int offset, int length) throws IOException {
    checkNotClosed();
    // Check the arguments, according to the HDFS contract.
    if (position < 0) {
      throw new EOFException("position is negative");
    }
    FSUtils.checkReadParameters(buffer, offset, length);
    if (length == 0) {
      return 0;
    }

    if (contentLength == 0 || position >= contentLength) {
      return -1;
    }

    long remaining = contentLength - position;
    int limit = (remaining >= length) ? length : (int) remaining;

    try (InputStream in = storage.get(objectKey, position, limit).verifiedStream(checksum)) {
      return in.read(buffer, offset, limit);
    }
  }

View on GitHub (pinned to 2add963021)

Solutions

  1. Check position >= 0 (and < contentLength when relevant) before the call
  2. Clamp position and length to contentLength - position in range loops
  3. Return -1 for out-of-range positions instead of calling the stream with negative values

Example fix

// before
int n = in.read(pos, buf, off, len);

// after
if (pos < 0) {
  return -1;
}
int n = in.read(pos, buf, off, len);
Defensive patterns

Strategy: validation

Validate before calling

if (position < 0) {
  return -1; // or throw IllegalArgumentException with context
}
int n = in.read(position, buffer, offset, length);

Prevention

When it happens

Trigger: Calling read(position, buf, off, len) with a negative position — typically an underflowed offset (position - length) or a -1 'unknown position' value passed straight through from another API.

Common situations: RecordReader/split math computing a negative start offset; loops of positioned reads where the remaining-length calculation goes negative past EOF.

Related errors


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