{"record":{"id":"4525524d20e0c4cc","repo":"apache/dubbo","slug":"base642bytes-base64-string-length-error","errorCode":null,"errorMessage":"base642bytes: base64 string length error.","messagePattern":"base642bytes: base64 string length error\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":642,"sourceCode":"            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);\n            if (str.charAt(off + len - 2) == pc) {\n                size -= 2;\n                --num;\n                rem = 2;\n            } else if (str.charAt(off + len - 1) == pc) {\n                size--;\n                --num;\n                rem = 3;\n            }\n        } else {\n            if (rem == 2) {\n                size++;\n            } else if (rem == 3) {\n                size += 2;\n            }","sourceCodeStart":624,"sourceCodeEnd":660,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L624-L660","documentation":"Thrown by the String-alphabet Base64 decoder only when the alphabet includes a pad character (code.length() > 64) AND the segment length `len` is not a multiple of 4. Padded Base64 (RFC 4648 style with '=') must always be a multiple of 4 characters; a non-multiple length with a pad-capable alphabet is malformed and is rejected with IllegalArgumentException. (Without a pad char, trailing groups of 2 or 3 chars are allowed.)","triggerScenarios":"Decoding with a padded alphabet (e.g. the default C64 which has '=' at index 64) a string whose length is not a multiple of 4, e.g. 'ZHViYm8' (7 chars) instead of 'ZHViYm8=' (8).","commonSituations":"Padding '=' characters were stripped (URL/cookie transport that drops '='); concatenation of Base64 fragments without re-padding; config/property value trimmed of trailing '='.","solutions":["Restore standard padding: append '=' until length is a multiple of 4 before decoding.","If you control the alphabet, decode with a no-pad alphabet (exactly 64 chars) which tolerates trailing groups of 2 or 3.","Sanitize untrusted Base64: s = s.replaceAll(\"[^A-Za-z0-9+/]\", \"\"); then re-pad to a multiple of 4."],"exampleFix":"// before\nbyte[] b = Bytes.base642bytes(\"ZHViYm8\", 0, 7, Bytes.C64); // 7 not multiple of 4\n// after\nString s = \"ZHViYm8\";\nwhile (s.length() % 4 != 0) s += \"=\";\nbyte[] b = Bytes.base642bytes(s, 0, s.length(), Bytes.C64);","handlingStrategy":"validation","validationCode":"String s = str.substring(off, off + len);\nif (Bytes.C64.length() > 64 && s.length() % 4 != 0) {\n    StringBuilder sb = new StringBuilder(s);\n    while (sb.length() % 4 != 0) sb.append('=');\n    s = sb.toString();\n    off = 0; len = s.length();\n}\nbyte[] b = Bytes.base642bytes(s, off, len, Bytes.C64);","typeGuard":"static boolean isPaddedBase64(String s) {\n    return s.length() % 4 == 0;\n}","tryCatchPattern":"try {\n    byte[] b = Bytes.base642bytes(str, off, len, Bytes.C64);\n} catch (IllegalArgumentException e) {\n    String s = str.substring(off, off + len);\n    while (s.length() % 4 != 0) s += \"=\";\n    byte[] b = Bytes.base642bytes(s, 0, s.length(), Bytes.C64);\n}","preventionTips":["Re-pad stripped '=' before decoding with a padded alphabet.","Use a no-pad (64-char) alphabet if your transport strips padding.","Sanitize base64 from URLs/cookies: strip non-alphabet chars then re-pad."],"tags":["base64","decoding","format","padding","validation","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}