{"record":{"id":"c8d003766e9a4e6f","repo":"apache/hadoop","slug":"invalid-read-parameters-buf-length-d-off-d-le","errorCode":null,"errorMessage":"Invalid read parameters: buf.length=%d, off=%d, len=%d","messagePattern":"Invalid read parameters: buf\\.length=(.+?), off=(.+?), len=(.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-bos/src/main/java/org/apache/hadoop/fs/bos/BosInputStream.java","lineNumber":154,"sourceCode":"   * @throws IOException if an I/O error occurs\n   */\n  public synchronized int read(\n      byte[] buf, int off, int len) throws IOException {\n    checkNotClosed();\n\n    if (len == 0) {\n      return 0;\n    }\n\n    if (this.contentLength == 0\n        || (nextReadPos >= contentLength)) {\n      return -1;\n    }\n\n    // Validate and adjust parameters\n    if (off < 0 || len < 0\n        || len > buf.length - off) {\n      throw new IndexOutOfBoundsException(\n          String.format(\n              \"Invalid read parameters:\"\n                  + \" buf.length=%d, off=%d, len=%d\",\n              buf.length, off, len));\n    }\n\n    try {\n      lazySeek(nextReadPos, len);\n    } catch (EOFException e) {\n      // the end of the file has moved\n      return -1;\n    }\n\n    int bytesRead;\n    try {\n      bytesRead = in.read(buf, off, len);\n    } catch (EOFException e) {\n      onReadFailure(e, len);","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-bos/src/main/java/org/apache/hadoop/fs/bos/BosInputStream.java#L136-L172","documentation":"BosInputStream.read(byte[],int,int) validates the caller's buffer parameters before any network I/O and throws IndexOutOfBoundsException (formatted with buf.length, off, len) when off < 0, len < 0, or len > buf.length - off. It mirrors the standard InputStream contract: this is a caller programming error, not an environmental failure, and it happens before lazySeek so nothing is read.","triggerScenarios":"Calling read(buf, off, len) with a negative offset, negative length, or off+len beyond the buffer: e.g. read(buffer, buffer.length, 1024), or an offset variable that decremented past zero in a hand-rolled read loop.","commonSituations":"Custom RecordReader/InputFormat bugs; buffered read loops that compute 'remaining' incorrectly; passing a length taken from a header or protocol field without clamping it to the destination buffer size.","solutions":["Clamp before calling: int n = Math.min(len, buf.length - off); and assert off >= 0","Replace hand-rolled loops with InputStream.readNBytes(byte[],int,int)/readAllBytes or DataInputStream, which handle bounds correctly","If the exception fired, fix the caller's arithmetic — the values in the message show exactly which argument is wrong"],"exampleFix":"// before\nint r = in.read(buf, off, len); // off/len from protocol, unchecked\n\n// after\nif (off < 0 || len < 0 || off + len > buf.length) {\n  throw new IllegalArgumentException(\"bad read args\");\n}\nint r = in.read(buf, off, Math.min(len, buf.length - off));","handlingStrategy":"validation","validationCode":"static void checkReadArgs(byte[] buf, int off, int len) {\n  if (buf == null) throw new NullPointerException(\"buf\");\n  if (off < 0 || len < 0 || len > buf.length - off) {\n    throw new IllegalArgumentException(\n        \"off=\" + off + \" len=\" + len + \" buf.length=\" + buf.length);\n  }\n}\n// call checkReadArgs(buf, off, len) before in.read(buf, off, len)","typeGuard":null,"tryCatchPattern":"catch (IndexOutOfBoundsException e) {\n  // caller bug: values in the message identify the bad argument; fix at call site, do not retry\n  throw new IllegalArgumentException(\"bad read arguments\", e);\n}","preventionTips":["Prefer readNBytes/readAllBytes over manual offset loops","Clamp protocol-derived lengths to the destination buffer size","Unit-test read loops with boundary off/len values"],"tags":["bos","input-stream","index-out-of-bounds","argument-validation","hadoop"],"backgroundTag":"index-out-of-bounds","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}