{"record":{"id":"0c3f219acda97385","repo":"apache/dubbo","slug":"base642bytes-offset-length-string-length","errorCode":null,"errorMessage":"base642bytes: offset + length > string length.","messagePattern":"base642bytes: offset \\+ length > string length\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":627,"sourceCode":"     *\n     * @param str  base64 string.\n     * @param off  offset.\n     * @param len  length.\n     * @param code base64 code(0-63 is base64 char,64 is pad char).\n     * @return byte array.\n     */\n    public static byte[] base642bytes(final String str, final int off, final int len, final String code) {\n        if (off < 0) {\n            throw new IndexOutOfBoundsException(\"base642bytes: offset < 0, offset is \" + off);\n        }\n        if (len < 0) {\n            throw new IndexOutOfBoundsException(\"base642bytes: length < 0, length is \" + len);\n        }\n        if (len == 0) {\n            return new byte[0];\n        }\n        if (off + len > str.length()) {\n            throw new IndexOutOfBoundsException(\"base642bytes: offset + length > string length.\");\n        }\n\n        if (code.length() < 64) {\n            throw new IllegalArgumentException(\"Base64 code length < 64.\");\n        }\n\n        int rem = len % 4;\n        if (rem == 1) {\n            throw new IllegalArgumentException(\"base642bytes: base64 string length % 4 == 1.\");\n        }\n\n        int num = len / 4, size = num * 3;\n        if (code.length() > 64) {\n            if (rem != 0) {\n                throw new IllegalArgumentException(\"base642bytes: base64 string length error.\");\n            }\n\n            char pc = code.charAt(64);","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L609-L645","documentation":"Thrown by the String-alphabet Base64 decoder Bytes.base642bytes(String, int off, int len, String code) when off + len exceeds str.length(). The decoder indexes str.charAt(off + ...) up to off+len-1, so an out-of-range region would otherwise throw a raw StringIndexOutOfBoundsException in the decode loop; this guard surfaces a clear message instead.","triggerScenarios":"Passing offset/length whose sum exceeds the string length, e.g. base642bytes(str, 2, str.length(), code) (offset not subtracted); a length copied from a longer source string.","commonSituations":"Reusing a length from a larger original after substring; computing remaining bytes as str.length() instead of str.length() - off; truncated/corrupted length on the wire.","solutions":["Derive the region from the string: base642bytes(str, off, str.length() - off, code).","Use base642bytes(str) / base642bytes(str, code) so the string drives the bounds.","Precondition: assert off + len <= str.length() at the producer."],"exampleFix":"// before\nbyte[] b = Bytes.base642bytes(str, off, str.length(), C64); // off double-counted\n// after\nbyte[] b = Bytes.base642bytes(str, off, str.length() - off, C64);","handlingStrategy":"validation","validationCode":"if (off < 0 || len < 0 || off + len > str.length()) {\n    throw new IllegalArgumentException(\"bad region off=\" + off + \" len=\" + len + \" str=\" + str.length());\n}\nbyte[] b = Bytes.base642bytes(str, off, len, Bytes.C64);","typeGuard":"static boolean validRegion(String s, int off, int len) {\n    return off >= 0 && len >= 0 && off + len <= s.length();\n}","tryCatchPattern":"try {\n    byte[] b = Bytes.base642bytes(str, off, len, Bytes.C64);\n} catch (IndexOutOfBoundsException e) {\n    throw new IllegalArgumentException(\"decode region out of bounds\", e);\n}","preventionTips":["Compute remaining as str.length() - off.","Let the string drive bounds via base642bytes(str).","Validate region at the producer when offset/length arrive from external data."],"tags":["base64","decoding","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"}