{"record":{"id":"1e6e9ef76befeefc","repo":"apache/hadoop","slug":"position-is-negative","errorCode":null,"errorMessage":"position is negative","messagePattern":"position is negative","errorType":"exception","errorClass":"EOFException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/object/ObjectMultiRangeInputStream.java","lineNumber":147,"sourceCode":"      if (n < 0) {\n        return total == 0 ? -1 : total;\n      }\n\n      total += n;\n      offset += n;\n      currPos += n;\n      nextPos += n;\n    }\n\n    return total;\n  }\n\n  @Override\n  public int read(long position, byte[] buffer, int offset, int length) throws IOException {\n    checkNotClosed();\n    // Check the arguments, according to the HDFS contract.\n    if (position < 0) {\n      throw new EOFException(\"position is negative\");\n    }\n    FSUtils.checkReadParameters(buffer, offset, length);\n    if (length == 0) {\n      return 0;\n    }\n\n    if (contentLength == 0 || position >= contentLength) {\n      return -1;\n    }\n\n    long remaining = contentLength - position;\n    int limit = (remaining >= length) ? length : (int) remaining;\n\n    try (InputStream in = storage.get(objectKey, position, limit).verifiedStream(checksum)) {\n      return in.read(buffer, offset, limit);\n    }\n  }\n","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/object/ObjectMultiRangeInputStream.java#L129-L165","documentation":"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.","triggerScenarios":"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.","commonSituations":"RecordReader/split math computing a negative start offset; loops of positioned reads where the remaining-length calculation goes negative past EOF.","solutions":["Check position >= 0 (and < contentLength when relevant) before the call","Clamp position and length to contentLength - position in range loops","Return -1 for out-of-range positions instead of calling the stream with negative values"],"exampleFix":"// before\nint n = in.read(pos, buf, off, len);\n\n// after\nif (pos < 0) {\n  return -1;\n}\nint n = in.read(pos, buf, off, len);","handlingStrategy":"validation","validationCode":"if (position < 0) {\n  return -1; // or throw IllegalArgumentException with context\n}\nint n = in.read(position, buffer, offset, length);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check position >= 0 before every positioned read","Clamp position/length against contentLength in range loops","Do not feed -1 'unknown' values from other APIs into read(position,...)"],"tags":["hadoop-tos","object-storage","input-stream","positioned-read","negative-offset"],"backgroundTag":"negative-read-position","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}