{"record":{"id":"456436fd60626722","repo":"apache/hadoop","slug":"write-b-b-length-off-len","errorCode":null,"errorMessage":"write (b[{b.length}], {off}, {len})","messagePattern":"write \\(b\\[(.+?)\\], (.+?), (.+?)\\)","errorType":"validation","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/store/DataBlocks.java","lineNumber":115,"sourceCode":"  }\n\n  /**\n   * Validate args to a write command. These are the same validation checks\n   * expected for any implementation of {@code OutputStream.write()}.\n   *\n   * @param b   byte array containing data.\n   * @param off offset in array where to start.\n   * @param len number of bytes to be written.\n   * @throws NullPointerException      for a null buffer\n   * @throws IndexOutOfBoundsException if indices are out of range\n   * @throws IOException raised on errors performing I/O.\n   */\n  public static void validateWriteArgs(byte[] b, int off, int len)\n      throws IOException {\n    Preconditions.checkNotNull(b);\n    if ((off < 0) || (off > b.length) || (len < 0) ||\n        ((off + len) > b.length) || ((off + len) < 0)) {\n      throw new IndexOutOfBoundsException(\n          \"write (b[\" + b.length + \"], \" + off + \", \" + len + ')');\n    }\n  }\n\n  /**\n   * Create a factory.\n   *\n   * @param keyToBufferDir Key to buffer directory config for a FS.\n   * @param configuration  factory configurations.\n   * @param name           factory name -the option from {@link CommonConfigurationKeys}.\n   * @return the factory, ready to be initialized.\n   * @throws IllegalArgumentException if the name is unknown.\n   */\n  public static BlockFactory createFactory(String keyToBufferDir,\n      Configuration configuration,\n      String name) {\n    LOG.debug(\"Creating DataFactory of type : {}\", name);\n    switch (name) {","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/store/DataBlocks.java#L97-L133","documentation":"DataBlocks.validateWriteArgs enforces the standard OutputStream.write(b, off, len) contract for the block-based buffering used by filesystem upload paths (e.g. S3A fast upload): a null buffer throws NullPointerException; negative off/len, off beyond the buffer, or off + len exceeding b.length (including int overflow, checked via (off + len) < 0) throws IndexOutOfBoundsException('write (b[<len>], <off>, <len>)').","triggerScenarios":"Block write calls with off + len > b.length; negative off or len; (off + len) wrapping past Integer.MAX_VALUE; passing length = b.length while also passing a non-zero offset.","commonSituations":"Custom OutputStreams delegating with adjusted indices; loops that advance an offset but keep reusing the full array length; huge buffers triggering int overflow.","solutions":["Fix the arithmetic so 0 <= off <= off + len <= b.length","Clamp the delegated length to b.length - off","Compute off + len in long when either value can approach Integer.MAX_VALUE"],"exampleFix":"// before\nblock.write(data, offset, data.length); // off+len can exceed length\n\n// after\nblock.write(data, offset, data.length - offset);","handlingStrategy":"validation","validationCode":"static boolean validWriteBounds(byte[] b, int off, int len) {\n  return b != null && off >= 0 && len >= 0\n      && (long) off + (long) len <= b.length;\n}","typeGuard":null,"tryCatchPattern":"Catch IndexOutOfBoundsException (message 'write (b[...], off, len)') from block writes; it identifies argument math errors — fix the caller's off/len computation instead of retrying.","preventionTips":["Use (long) arithmetic when checking off + len near Integer.MAX_VALUE","Clamp delegated lengths to b.length - off","Do not pass full-array lengths together with a non-zero offset"],"tags":["hadoop","io","output-stream","buffer","index-out-of-bounds","upload"],"backgroundTag":"invalid-buffer-offset-length","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}