{"record":{"id":"c0be284c41913f0c","repo":"MuntashirAkon/AppManager","slug":"offs-offs-0-bzip2compressoroutputstream","errorCode":null,"errorMessage":"offs(\" + offs + \") < 0.","messagePattern":"offs\\(\" \\+ offs \\+ \"\\) < 0\\.","errorType":"validation","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java","lineNumber":607,"sourceCode":"        bsPutUByte(0x90);\n\n        bsPutInt(this.combinedCRC);\n        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","sourceCodeStart":589,"sourceCodeEnd":625,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java#L589-L625","documentation":"write(byte[] buf, int offs, int len) validates its arguments per the OutputStream contract: negative offsets, negative lengths, or offs+len beyond the buffer throw IndexOutOfBoundsException. These are caller bugs, not stream-state issues.","triggerScenarios":"Calling write(buf, offs, len) with offs < 0; also len < 0 or offs + len > buf.length (the adjacent checks). Commonly from a loop computing wrong offsets (e.g. offs += len before advancing) or misread read() results.","commonSituations":"Manual buffer-chunking loops with off-by-one arithmetic, passing -1 returned from an upstream read() as len, wrong variables swapped in the call.","solutions":["Fix the offset arithmetic so offs >= 0 and offs + len <= buf.length.","Check upstream read()/position results for -1 (EOF) before passing them as len.","Use safer helpers (e.g. buf, 0, n from InputStream.read) instead of hand-rolled offsets.","Optionally validate arguments with Objects.checkFromIndexRange-style checks before calling."],"exampleFix":"// before\nint n = in.read(buf);\ncompressor.write(buf, offs, n); // offs can be -1 / stale\n// after\nint n = in.read(buf);\nif (n > 0) compressor.write(buf, 0, n);","handlingStrategy":"validation","validationCode":"// standard bounds check before writing\nif (offs < 0 || len < 0 || offs + len > buf.length)\n    throw new IndexOutOfBoundsException(\"offs=\" + offs + \" len=\" + len + \" buf.length=\" + buf.length);","typeGuard":"static boolean validSlice(byte[] buf, int offs, int len) {\n    return buf != null && offs >= 0 && len >= 0 && offs + len <= buf.length;\n}","tryCatchPattern":"try {\n    compressor.write(buf, offs, len);\n} catch (IndexOutOfBoundsException e) {\n    throw new IllegalArgumentException(\"bad write slice: \" + e.getMessage(), e);\n}","preventionTips":["Check read() return for -1 before using it as len","Use (buf, 0, n) chunking instead of manual offset arithmetic","Reuse IOUtils/copy utilities rather than hand-rolled loops","Add assertions on offs/len in compression pipeline code"],"tags":["io","bzip2","index-out-of-bounds","argument-validation"],"backgroundTag":"index-out-of-bounds","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"}