{"record":{"id":"6bd07f7bd91b56e6","repo":"apache/hadoop","slug":"requested-more-bytes-than-destination-buffer-size-6bd07f","errorCode":null,"errorMessage":"Requested more bytes than destination buffer size: request length ={length}, with offset ={offset}; buffer capacity ={b.length - offset}","messagePattern":"Requested more bytes than destination buffer size: request length =(.+?), with offset =(.+?); buffer capacity =(.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/store/ByteBufferInputStream.java","lineNumber":175,"sourceCode":"  }\n\n  /**\n   * Read in data.\n   * @param b destination buffer.\n   * @param offset offset within the buffer.\n   * @param length length of bytes to read.\n   * @throws EOFException if the position is negative\n   * @throws IndexOutOfBoundsException if there isn't space for the\n   * amount of data requested.\n   * @throws IllegalArgumentException other arguments are invalid.\n   */\n  @SuppressWarnings(\"NullableProblems\")\n  public synchronized int read(byte[] b, int offset, int length)\n      throws IOException {\n    Preconditions.checkArgument(length >= 0, \"length is negative\");\n    Preconditions.checkArgument(b != null, \"Null buffer\");\n    if (b.length - offset < length) {\n      throw new IndexOutOfBoundsException(\n          FSExceptionMessages.TOO_MANY_BYTES_FOR_DEST_BUFFER\n              + \": request length =\" + length\n              + \", with offset =\" + offset\n              + \"; buffer capacity =\" + (b.length - offset));\n    }\n    verifyOpen();\n    if (!hasRemaining()) {\n      return -1;\n    }\n\n    int toRead = Math.min(length, available());\n    byteBuffer.get(b, offset, toRead);\n    return toRead;\n  }\n\n  @Override\n  public String toString() {\n    return \"ByteBufferInputStream{\" +","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/store/ByteBufferInputStream.java#L157-L193","documentation":"The positional read read(b, offset, length) first rejects a negative length and a null buffer (Preconditions IllegalArgumentException), then checks destination capacity: if b.length - offset < length it throws IndexOutOfBoundsException with 'Requested more bytes than destination buffer size' plus the request length, offset and capacity. verifyOpen() runs after these checks, so a bounds error can surface even on a closed stream.","triggerScenarios":"read(buf, off, len) with len greater than buf.length - off; reusing a non-zero offset with a buffer smaller than offset + length.","commonSituations":"Buffer math copied from code where offset was 0; reads sized by a record length into a header-sized buffer; off-by-one in loop bounds.","solutions":["Allocate the destination with at least offset + length bytes","Clamp the request: len = Math.min(len, b.length - offset)","Assert b.length - offset >= length in debug paths before the call"],"exampleFix":"// before\nbyte[] small = new byte[16];\nin.read(small, 8, 32); // capacity 8 < requested 32\n\n// after\nbyte[] buf = new byte[8 + 32];\nin.read(buf, 8, 32);","handlingStrategy":"validation","validationCode":"int maxLen = b.length - offset;\nif (length > maxLen) {\n  length = maxLen; // or throw your own clearer error\n}\nint n = in.read(b, offset, length);","typeGuard":null,"tryCatchPattern":"Catch IndexOutOfBoundsException from read(byte[], int, int) and report buffer capacity versus request length; the message already contains all three numbers.","preventionTips":["Size buffers as offset + maxRead","Clamp lengths to b.length - offset","Keep offset at 0 unless the buffer was sized for the offset"],"tags":["hadoop","io","input-stream","buffer","index-out-of-bounds"],"backgroundTag":"destination-buffer-too-small","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}