{"record":{"id":"92082acd14e856de","repo":"MuntashirAkon/AppManager","slug":"array-size-array-length-offset-off-length-len","errorCode":null,"errorMessage":"Array Size=\" + array.length + \", offset=\" + off + \", length=\" + len","messagePattern":"Array Size=\" \\+ array\\.length \\+ \", offset=\" \\+ off \\+ \", length=\" \\+ len","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/CharSequenceInputStream.java","lineNumber":171,"sourceCode":"                return mByteBuffer.get() & 0xFF;\n            }\n            fillBuffer();\n            if (!mByteBuffer.hasRemaining() && !mCharBuffer.hasRemaining()) {\n                return EOF;\n            }\n        }\n    }\n\n    @Override\n    public int read(final byte[] b) throws IOException {\n        return read(b, 0, b.length);\n    }\n\n    @Override\n    public int read(final byte[] array, int off, int len) throws IOException {\n        Objects.requireNonNull(array, \"array\");\n        if (len < 0 || off + len > array.length) {\n            throw new IndexOutOfBoundsException(\"Array Size=\" + array.length + \", offset=\" + off + \", length=\" + len);\n        }\n        if (len == 0) {\n            return 0; // must return 0 for zero length read\n        }\n        if (!mByteBuffer.hasRemaining() && !mCharBuffer.hasRemaining()) {\n            return EOF;\n        }\n        int bytesRead = 0;\n        while (len > 0) {\n            if (mByteBuffer.hasRemaining()) {\n                final int chunk = Math.min(mByteBuffer.remaining(), len);\n                mByteBuffer.get(array, off, chunk);\n                off += chunk;\n                len -= chunk;\n                bytesRead += chunk;\n            } else {\n                fillBuffer();\n                if (!mByteBuffer.hasRemaining() && !mCharBuffer.hasRemaining()) {","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/CharSequenceInputStream.java#L153-L189","documentation":"CharSequenceInputStream.read(byte[], off, len) validates the caller-supplied array bounds and throws IndexOutOfBoundsException when off is negative, len is negative, or off+len exceeds array.length. This is a standard buffer-bounds contract guard before encoding chars into bytes.","triggerScenarios":"Calling read(array, off, len) with off + len > array.length, or negative off/len. The SOURCE shows the check `if (len < 0 || off + len > array.length)`.","commonSituations":"Misreading the API as (offset, count-of-remaining) and passing len = array.length while off > 0; arithmetic errors computing remaining bytes; passing a zero-length buffer with nonzero off.","solutions":["Ensure off >= 0, len >= 0, and off + len <= array.length before calling read().","Clamp len to Math.min(len, array.length - off) when reading at a nonzero offset.","Use the stream's single-argument read() or readAllBytes-style helpers to avoid manual offset math."],"exampleFix":"// before\nint n = stream.read(buf, off, buf.length);\n// after\nint n = stream.read(buf, off, Math.min(buf.length - off, buf.length));","handlingStrategy":"validation","validationCode":"static boolean readableRange(byte[] a, int off, int len) {\n    return a != null && off >= 0 && len >= 0 && off + len <= a.length;\n}","typeGuard":null,"tryCatchPattern":"if (!readableRange(buf, off, len)) throw new IllegalArgumentException(\"bad off/len\");\nint n = stream.read(buf, off, len);","preventionTips":["Clamp len with array.length - off","Prefer offset-free read APIs when possible","Unit-test read loops at buffer boundaries"],"tags":["io","bounds","encoding"],"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-23T08:17:48.524Z"}