{"record":{"id":"935267eead71d997","repo":"apache/hadoop","slug":"requested-more-bytes-than-destination-buffer-size","errorCode":null,"errorMessage":"Requested more bytes than destination buffer size: request length ={}, with offset ={}; buffer capacity ={}","messagePattern":"Requested more bytes than destination buffer size: request length =(.+?), with offset =(.+?); buffer capacity =(.+?)","errorType":"validation","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-huaweicloud/src/main/java/org/apache/hadoop/fs/obs/OBSDataBlocks.java","lineNumber":783,"sourceCode":"      /**\n       * Read in data.\n       *\n       * @param b      destination buffer\n       * @param offset offset within the buffer\n       * @param length length of bytes to read\n       * @return read size\n       * @throws EOFException              if the position is negative\n       * @throws IndexOutOfBoundsException if there isn't space for the amount\n       *                                   of data requested.\n       * @throws IllegalArgumentException  other arguments are invalid.\n       */\n      public synchronized int read(final byte[] b, final int offset,\n          final 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 =\"\n                  + length\n                  + \", with offset =\"\n                  + offset\n                  + \"; buffer capacity =\"\n                  + (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","sourceCodeStart":765,"sourceCodeEnd":801,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-huaweicloud/src/main/java/org/apache/hadoop/fs/obs/OBSDataBlocks.java#L765-L801","documentation":"In the block buffer's read(byte[] b, int offset, int length), a precondition asserts the destination array has room: if b.length - offset < length it throws IndexOutOfBoundsException with FSExceptionMessages.TOO_MANY_BYTES_FOR_DEST_BUFFER plus the request length, offset, and capacity. This mirrors java.io.DataInputStream semantics: the caller asked to read more bytes than the destination slice can hold. Preconditions on length>=0 and non-null b are checked just before, so those produce IllegalArgumentException instead.","triggerScenarios":"Calling read(buf, offset, length) where offset+length > buf.length (e.g. buf=new byte[1024], offset=1000, length=100); reusing a shared buffer with a stale length from a previous, larger buffer; passing offset equal to buf.length with length>0.","commonSituations":"Off-by-one arithmetic in custom InputStream consumers; buffer-pool code that swaps in smaller buffers but keeps lengths computed against the old size; interop code that assumes read() clips the request instead of throwing.","solutions":["Validate arguments before the call: require offset >= 0, length >= 0, and offset + length <= b.length","Compute the requested length as min(length, b.length - offset) so it is always clipped to the destination slice","Add unit tests for boundary offsets (offset = b.length, length = 0 is legal; offset+length = b.length is the max)"],"exampleFix":"// before\nint n = blockStream.read(buf, offset, length); // throws IndexOutOfBoundsException\n\n// after\nint maxLen = Math.min(length, buf.length - offset);\nint n = blockStream.read(buf, offset, maxLen);","handlingStrategy":"validation","validationCode":"void checkReadArgs(byte[] b, int off, int len) {\n  if (b == null) throw new IllegalArgumentException(\"Null buffer\");\n  if (len < 0) throw new IllegalArgumentException(\"length is negative\");\n  if (off < 0 || b.length - off < len) {\n    throw new IllegalArgumentException(\n        \"offset+length exceeds buffer: off=\" + off + \", len=\" + len + \", cap=\" + b.length);\n  }\n}\ncheckReadArgs(buf, offset, length);\nint n = blockStream.read(buf, offset, length);","typeGuard":null,"tryCatchPattern":"try {\n  n = blockStream.read(buf, offset, length);\n} catch (IndexOutOfBoundsException e) {\n  // caller bug: fix argument arithmetic; do not retry\n  throw new IllegalArgumentException(\"bad read args: off=\" + offset + \", len=\" + length + \", cap=\" + buf.length, e);\n}","preventionTips":["Compute lengths against the actual buffer instance passed (not a stale constant)","Clip requests: len = min(len, b.length - off) at API boundaries","Test boundary offsets 0, b.length, and offset+length == b.length"],"tags":["invalid-argument","buffer-bounds","io","hadoop-obs"],"backgroundTag":"buffer-overflow-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}