{"record":{"id":"8cf94e8e624ecc6f","repo":"apache/hadoop","slug":"invalid-capacity-limit","errorCode":null,"errorMessage":"Invalid capacity/limit","messagePattern":"Invalid capacity/limit","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/BoundedByteArrayOutputStream.java","lineNumber":66,"sourceCode":"\n  /**\n   * Create a BoundedByteArrayOutputStream with the specified\n   * capacity and limit.\n   * @param capacity The capacity of the underlying byte array\n   * @param limit The maximum limit upto which data can be written\n   */\n  public BoundedByteArrayOutputStream(int capacity, int limit) {\n    this(new byte[capacity], 0, limit);\n  }\n\n  protected BoundedByteArrayOutputStream(byte[] buf, int offset, int limit) {\n    resetBuffer(buf, offset, limit);\n  }\n  \n  protected void resetBuffer(byte[] buf, int offset, int limit) {\n    int capacity = buf.length - offset;\n    if ((capacity < limit) || (capacity | limit) < 0) {\n      throw new IllegalArgumentException(\"Invalid capacity/limit\");\n    }\n    this.buffer = buf;\n    this.startOffset = offset;\n    this.currentPointer = offset;\n    this.limit = offset + limit;\n  }\n  \n  @Override\n  public void write(int b) throws IOException {\n    if (currentPointer >= limit) {\n      throw new EOFException(\"Reaching the limit of the buffer.\");\n    }\n    buffer[currentPointer++] = (byte) b;\n  }\n\n  @Override\n  public void write(byte b[], int off, int len) throws IOException {\n    if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length)","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/BoundedByteArrayOutputStream.java#L48-L84","documentation":"BoundedByteArrayOutputStream writes into a fixed byte buffer and refuses construction/reset with a limit larger than the usable capacity, or with negative values. resetBuffer() throws IllegalArgumentException when capacity (buf.length - offset) is less than limit or when either is negative ((capacity | limit) < 0 catches both negative).","triggerScenarios":"new BoundedByteArrayOutputStream(10, 20) — capacity 10 with limit 20; passing a negative limit or offset such that usable capacity is negative; subclass constructors forwarding bad (buf, offset, limit) triples.","commonSituations":"Sizing shuffle/merge buffers (e.g. mapreduce task output buffers) from configuration where limit and capacity are computed independently; passing an offset larger than the buffer length when reusing a shared buffer.","solutions":["Ensure limit <= capacity: allocate new byte[limit] if you need the full limit, i.e. new BoundedByteArrayOutputStream(limit, limit).","Validate computed sizes against zero before constructing and clamp or fail with context (which config key was bad).","When reusing a buffer with an offset, recompute capacity as buf.length - offset and size the limit within it."],"exampleFix":"// before\nint limit = conf.getInt(\"my.limit\", 64 * 1024);\nnew BoundedByteArrayOutputStream(16 * 1024, limit); // throws when limit > 16K\n\n// after\nint limit = Math.min(conf.getInt(\"my.limit\", 64 * 1024), 16 * 1024);\nnew BoundedByteArrayOutputStream(16 * 1024, limit);","handlingStrategy":"validation","validationCode":"int limit = Math.min(configuredLimit, capacity);\nif (capacity <= 0 || limit < 0) {\n  throw new IllegalArgumentException(\"capacity=\" + capacity + \", limit=\" + limit);\n}\nnew BoundedByteArrayOutputStream(capacity, limit);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive capacity and limit from a single sizing constant/config so they cannot diverge.","Treat any limit > capacity as a configuration bug and validate it in job setup."],"tags":["hadoop","io","buffer","validation","capacity"],"backgroundTag":"invalid-buffer-capacity","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}