{"record":{"id":"5988a6a99d5bb5d4","repo":"apache/dubbo","slug":"bytes2base64-offset-length-array-length","errorCode":null,"errorMessage":"bytes2base64: offset + length > array length.","messagePattern":"bytes2base64: offset \\+ length > array length\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":534,"sourceCode":"\n    /**\n     * to base64 string.\n     *\n     * @param bs   byte array.\n     * @param off  offset.\n     * @param len  length.\n     * @param code base64 code(0-63 is base64 char,64 is pad char).\n     * @return base64 string.\n     */\n    public static String bytes2base64(final byte[] bs, final int off, final int len, final char[] code) {\n        if (off < 0) {\n            throw new IndexOutOfBoundsException(\"bytes2base64: offset < 0, offset is \" + off);\n        }\n        if (len < 0) {\n            throw new IndexOutOfBoundsException(\"bytes2base64: length < 0, length is \" + len);\n        }\n        if (off + len > bs.length) {\n            throw new IndexOutOfBoundsException(\"bytes2base64: offset + length > array length.\");\n        }\n\n        if (code.length < 64) {\n            throw new IllegalArgumentException(\"Base64 code length < 64.\");\n        }\n\n        boolean pad = code.length > 64; // has pad char.\n        int num = len / 3, rem = len % 3, r = off, w = 0;\n        char[] cs = new char[num * 4 + (rem == 0 ? 0 : pad ? 4 : rem + 1)];\n\n        for (int i = 0; i < num; i++) {\n            int b1 = bs[r++] & MASK8, b2 = bs[r++] & MASK8, b3 = bs[r++] & MASK8;\n\n            cs[w++] = code[b1 >> 2];\n            cs[w++] = code[(b1 << 4) & MASK6 | (b2 >> 4)];\n            cs[w++] = code[(b2 << 2) & MASK6 | (b3 >> 6)];\n            cs[w++] = code[b3 & MASK6];\n        }","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L516-L552","documentation":"Thrown by Bytes.bytes2base64(byte[], int off, int len, char[] code) when off + len exceeds bs.length, i.e. the requested encode region runs past the end of the byte array. It is a region-bounds precondition; the loop indexes bs[r++] up to off+len-1, so an out-of-range region would otherwise throw a raw ArrayIndexOutOfBoundsException deeper in the encode loop.","triggerScenarios":"Passing an offset/length pair whose sum exceeds the array length, e.g. bytes2base64(bs, 2, bs.length, code) (forgets to subtract the offset); or a length copied from a different, larger array.","commonSituations":"Reusing a length from a larger source buffer after sub-slicing; miscomputing remaining = total when an offset is also given (must be bs.length - off); truncation/corruption of a length field on the wire.","solutions":["Compute the region from the array itself: bytes2base64(bs, off, bs.length - off, code).","Use bytes2base64(bs) or bytes2base64(bs, off, bs.length - off) so the array drives the bounds.","Validate at the producer: assert off + len <= bs.length before forwarding."],"exampleFix":"// before\nString s = Bytes.bytes2base64(bs, off, bs.length, Bytes.BASE64); // double-counts off\n// after\nString s = Bytes.bytes2base64(bs, off, bs.length - off, Bytes.BASE64);","handlingStrategy":"validation","validationCode":"if (off < 0 || len < 0 || off + len > bs.length) {\n    throw new IllegalArgumentException(\"bad region off=\" + off + \" len=\" + len + \" arr=\" + bs.length);\n}\nString s = Bytes.bytes2base64(bs, off, len, Bytes.BASE64);","typeGuard":"static boolean validRegion(byte[] a, int off, int len) {\n    return off >= 0 && len >= 0 && off + len <= a.length;\n}","tryCatchPattern":"try {\n    String s = Bytes.bytes2base64(bs, off, len, Bytes.BASE64);\n} catch (IndexOutOfBoundsException e) {\n    throw new IllegalArgumentException(\"encode region out of bounds\", e);\n}","preventionTips":["Compute remaining as bs.length - off, never bs.length.","Let the array drive bounds via bytes2base64(byte[]).","Beware int overflow on off + len for very large arrays — they are unrealistic here but validate the producer."],"tags":["base64","encoding","bounds-check","validation","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}