{"record":{"id":"22bb97ff7d598081","repo":"apache/dubbo","slug":"base642bytes-length-0-length-is","errorCode":null,"errorMessage":"base642bytes: length < 0, length is {}","messagePattern":"base642bytes: length < 0, length is (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":621,"sourceCode":"    public static byte[] base642bytes(String str, String code) {\n        return base642bytes(str, 0, str.length(), code);\n    }\n\n    /**\n     * from base64 string.\n     *\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;","sourceCodeStart":603,"sourceCodeEnd":639,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L603-L639","documentation":"Thrown by the String-alphabet Base64 decoder Bytes.base642bytes(String, int off, int len, String code) when `len` is negative. Length drives the output byte-array sizing and the decode loop bound, so a negative length is invalid and rejected with IndexOutOfBoundsException up front. Convenience overloads that compute len from str.length() never trigger this.","triggerScenarios":"Calling base642bytes(str, off, len, code) with len < 0; computing len = end - start with end < start; passing -1 as a sentinel (none is supported here).","commonSituations":"Off-by-one in substring slicing; a length field that was never initialized (default -1); porting from an API where -1 meant 'whole string'.","solutions":["Use base642bytes(str) or base642bytes(str, code) to let the API derive len = str.length().","Compute defensively: int len = Math.max(0, end - start);","Surface bad producers with a precondition on the length source."],"exampleFix":"// before\nbyte[] b = Bytes.base642bytes(str, off, end - start, C64); // negative when end < start\n// after\nint len = Math.max(0, end - start);\nbyte[] b = Bytes.base642bytes(str, off, len, C64);","handlingStrategy":"validation","validationCode":"if (len < 0) throw new IllegalArgumentException(\"len must be >= 0, got \" + len);\nbyte[] b = Bytes.base642bytes(str, off, len, Bytes.C64);","typeGuard":null,"tryCatchPattern":"try {\n    byte[] b = Bytes.base642bytes(str, off, len, Bytes.C64);\n} catch (IndexOutOfBoundsException e) {\n    throw new IllegalArgumentException(\"invalid decode length\", e);\n}","preventionTips":["Derive length from str.length() - off rather than computing it separately.","Use base642bytes(str) when decoding the whole string.","Never pass -1 as a length."],"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"}