{"record":{"id":"aae95ba88096d3eb","repo":"apache/hadoop","slug":"len-0","errorCode":null,"errorMessage":"len({}) < 0.","messagePattern":"len\\((.+?)\\) < 0\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/bzip2/CBZip2OutputStream.java","lineNumber":866,"sourceCode":"    bsFinishedWithStream();\n  }\n\n  /**\n  * Returns the blocksize parameter specified at construction time.\n  * @return blocksize.\n  */\n  public final int getBlockSize() {\n    return this.blockSize100k;\n  }\n\n  @Override\n  public void write(final byte[] buf, int offs, final int len)\n      throws IOException {\n    if (offs < 0) {\n      throw new IndexOutOfBoundsException(\"offs(\" + offs + \") < 0.\");\n    }\n    if (len < 0) {\n      throw new IndexOutOfBoundsException(\"len(\" + len + \") < 0.\");\n    }\n    if (offs + len > buf.length) {\n      throw new IndexOutOfBoundsException(\"offs(\" + offs + \") + len(\"\n          + len + \") > buf.length(\" + buf.length + \").\");\n    }\n    if (this.out == null) {\n      throw new IOException(\"stream closed\");\n    }\n\n    for (int hi = offs + len; offs < hi;) {\n      write0(buf[offs++]);\n    }\n  }\n\n  private void write0(int b) throws IOException {\n    if (this.currentChar != -1) {\n      b &= 0xff;\n      if (this.currentChar == b) {","sourceCodeStart":848,"sourceCodeEnd":884,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/bzip2/CBZip2OutputStream.java#L848-L884","documentation":"Second guard in CBZip2OutputStream.write(byte[], offs, len): a negative length throws IndexOutOfBoundsException(\"len(...) < 0.\"). A negative length can never denote a valid byte range, so it is rejected immediately instead of being treated as zero or looping backwards through the buffer.","triggerScenarios":"write(buf, offs, len) with len < 0: remaining-bytes computations like (limit - pos) going negative after the cursor passed the limit, error returns (-1) from size/parse functions used as lengths, or swapped offs/len arguments putting a negative value in len.","commonSituations":"Read loops that compute remaining = total - written without checking sign when the stream ends early; APIs returning -1 for 'unknown/none' being forwarded as a length; argument-order mixups after refactors between (len, offs) styles.","solutions":["Check the length before calling: if (len < 0) throw new IllegalArgumentException(\"bad len \" + len);","Clamp semantics intentionally: treat 'nothing to write' as len == 0 (skip the call) instead of passing a negative remaining count.","Separate 'error' return values (-1) from valid lengths at the API boundary; never forward them.","Prefer Objects.checkFromToIndex(offs, offs + len, buf.length) for a single consistent bounds check."],"exampleFix":"// before\nint remaining = total - written; // can go negative on short reads\ncbz.write(buf, off, remaining);\n\n// after\nint remaining = Math.max(0, total - written);\nif (remaining > 0) {\n  cbz.write(buf, off, remaining);\n}","handlingStrategy":"validation","validationCode":"if (len < 0) {\n  throw new IllegalArgumentException(\"len=\" + len + \" for write()\");\n}\ncbz.write(buf, offs, len);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp 'remaining' computations with Math.max(0, ...) before writing.","Separate error codes (-1) from valid lengths at API boundaries.","Keep (buf, off, len) parameter order consistent across your wrappers."],"tags":["bzip2","compression","invalid-argument","bounds","hadoop"],"backgroundTag":"invalid-offset-length","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}