{"record":{"id":"1771b64cdaf9d762","repo":"MuntashirAkon/AppManager","slug":"len-len-0-bzip2compressoroutputstream","errorCode":null,"errorMessage":"len(\" + len + \") < 0.","messagePattern":"len\\(\" \\+ len \\+ \"\\) < 0\\.","errorType":"validation","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java","lineNumber":610,"sourceCode":"        bsFinishedWithStream();\n    }\n\n    /**\n     * Returns the blocksize parameter specified at construction time.\n     * @return the blocksize parameter specified at construction time\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(\"\n                    + buf.length + \").\");\n        }\n        if (closed) {\n            throw new IOException(\"Stream closed\");\n        }\n\n        for (final int hi = offs + len; offs < hi;) {\n            write0(buf[offs++]);\n        }\n    }\n\n    /**\n     * Keeps track of the last bytes written and implicitly performs\n     * run-length encoding as the first step of the bzip2 algorithm.","sourceCodeStart":592,"sourceCodeEnd":628,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java#L592-L628","documentation":"BZip2CompressorOutputStream.write(byte[], int, int) validates its arguments before writing and throws IndexOutOfBoundsException when len is negative. This mirrors the java.io.OutputStream contract so callers detect bad buffer ranges early instead of corrupting the compressed stream.","triggerScenarios":"Calling write(buf, offs, len) with len < 0, typically from a computed length like end-start where end < start, or an int overflow producing a negative count.","commonSituations":"Wrapping code that slices buffers with off-by-one index math; passing a remaining-bytes counter that already went negative; copying from a buffer shorter than expected.","solutions":["Fix the caller's length computation so len is never negative before invoking write.","Clamp or guard: only call write when len > 0 (skip the call when len == 0).","If indices come from external input, validate 0 <= len <= buf.length - offs upstream and fail with a clear message."],"exampleFix":"// before\nout.write(buf, off, end - off);\n// after\nint len = end - off;\nif (len > 0) {\n    out.write(buf, off, len);\n}","handlingStrategy":"validation","validationCode":"if (len < 0 || offs < 0 || offs + len > buf.length) {\n    throw new IllegalArgumentException(\"bad write range: offs=\" + offs + \" len=\" + len);\n}\nout.write(buf, offs, len);","typeGuard":null,"tryCatchPattern":"try {\n    out.write(buf, offs, len);\n} catch (IndexOutOfBoundsException e) {\n    // log offs/len/buf.length and fix the caller's range math\n}","preventionTips":["Compute lengths as differences in one place and assert non-negativity.","Skip the write call entirely when len <= 0.","Avoid int overflow by using long arithmetic when offsets can be large."],"tags":["io","argument-validation","bzip2"],"backgroundTag":"value-out-of-range","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}