{"record":{"id":"693db292b74327c4","repo":"apache/dubbo","slug":"base642bytes-base64-string-length-4-1","errorCode":null,"errorMessage":"base642bytes: base64 string length % 4 == 1.","messagePattern":"base642bytes: base64 string length % 4 == 1\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":636,"sourceCode":"            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);\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            }","sourceCodeStart":618,"sourceCodeEnd":654,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L618-L654","documentation":"Thrown by the String-alphabet Base64 decoder when the supplied segment length `len` satisfies len % 4 == 1. Every valid Base64 group encodes 3 bytes into 4 characters, so a total length congruent to 1 mod 4 is impossible for any well-formed Base64 (with or without padding) and is rejected with IllegalArgumentException before any decoding.","triggerScenarios":"Decoding a string/segment whose character count is 1, 5, 9, ... i.e. len % 4 == 1; a truncated Base64 payload missing several trailing chars; a region length computed incorrectly so it lands on 1 mod 4.","commonSituations":"Truncation of a Base64 blob in transit or in a property file; copy-paste that dropped trailing characters; an off/len slice that cut a 4-group mid-block leaving 1 char.","solutions":["Re-source the original Base64 string (it has been truncated/corrupted).","If you intentionally slice, slice on a 4-char boundary: len -= (len % 4) before decoding (only if dropping trailing chars is acceptable).","Validate/repair: pad with '=' to a multiple of 4 only when the input is known-padded and the alphabet has a pad char."],"exampleFix":"// before\nbyte[] b = Bytes.base642bytes(truncatedStr, 0, truncatedStr.length(), C64); // len % 4 == 1\n// after\n// re-fetch the full, untruncated Base64 string, then:\nbyte[] b = Bytes.base642bytes(fullStr, 0, fullStr.length(), C64);","handlingStrategy":"validation","validationCode":"if (len % 4 == 1) throw new IllegalArgumentException(\"not valid base64: len % 4 == 1\");\nbyte[] b = Bytes.base642bytes(str, off, len, Bytes.C64);","typeGuard":"static boolean isBase64LengthOk(int len) {\n    return len % 4 != 1;\n}","tryCatchPattern":"try {\n    byte[] b = Bytes.base642bytes(str, off, len, Bytes.C64);\n} catch (IllegalArgumentException e) {\n    // truncated input — re-fetch or reject\n    throw new IllegalArgumentException(\"base64 input truncated/corrupt\", e);\n}","preventionTips":["Treat len % 4 == 1 as corruption — re-source the data rather than patching.","Slice base64 on 4-char boundaries only.","Add a length/checksum sanity check upstream of decoding."],"tags":["base64","decoding","format","validation","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}