{"record":{"id":"a7c82de2383148f9","repo":"MuntashirAkon/AppManager","slug":"len-len-0","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/BZip2CompressorInputStream.java","lineNumber":140,"sourceCode":"            count(r < 0 ? -1 : 1);\n            return r;\n        }\n        throw new IOException(\"Stream closed\");\n    }\n\n    /*\n     * (non-Javadoc)\n     *\n     * @see java.io.InputStream#read(byte[], int, int)\n     */\n    @Override\n    public int read(final byte[] dest, final 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 > dest.length) {\n            throw new IndexOutOfBoundsException(\"offs(\" + offs + \") + len(\"\n                    + len + \") > dest.length(\" + dest.length + \").\");\n        }\n        if (this.bin == null) {\n            throw new IOException(\"Stream closed\");\n        }\n        if (len == 0) {\n            return 0;\n        }\n\n        final int hi = offs + len;\n        int destOffs = offs;\n        int b;\n        while (destOffs < hi && ((b = read0()) >= 0)) {\n            dest[destOffs++] = (byte) b;\n            count(1);","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java#L122-L158","documentation":"read(byte[], int, int) validates its bounds arguments before touching the stream and throws IndexOutOfBoundsException when len is negative. The library treats a negative length as a programming error in the caller, not an I/O condition, mirroring the contract of java.io.InputStream.","triggerScenarios":"Calling read(dest, offs, -1) or passing any negative length, typically from a computed value that became negative (e.g. remaining = size - offset underflow).","commonSituations":"Loop bookkeeping bugs where a buffer offset or remaining-bytes counter underflows; passing a user-supplied chunk size without clamping; off-by-one in a custom InputStream wrapper.","solutions":["Clamp or validate len >= 0 before calling read (e.g. len = Math.min(len, dest.length - offs)).","Check the counter computation feeding len for underflow (int arithmetic wrapping).","Use a loop pattern like while ((n = in.read(buf, 0, buf.length)) != -1) which never passes negative len."],"exampleFix":"// before\nint n = in.read(buf, offs, remaining - 1);\n// after\nif (remaining < 0) remaining = 0;\nint n = in.read(buf, offs, Math.max(0, remaining));","handlingStrategy":"validation","validationCode":"if (len < 0) throw new IllegalArgumentException(\"len must be >= 0\");\n// or clamp: len = Math.max(0, Math.min(len, dest.length - offs));","typeGuard":"static boolean isValidReadArgs(byte[] dest, int offs, int len) {\n    return dest != null && offs >= 0 && len >= 0 && offs + len <= dest.length;\n}","tryCatchPattern":null,"preventionTips":["Use the canonical read loop: while ((n = in.read(buf, 0, buf.length)) != -1).","Clamp computed lengths with Math.min/Math.max before calling read.","Watch for integer underflow in remaining-byte counters."],"tags":["io","argument-validation","indexoutofboundsexception"],"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"}