{"record":{"id":"a986b44dbd363dc3","repo":"apache/dubbo","slug":"base642bytes-offset-0-offset-is","errorCode":null,"errorMessage":"base642bytes: offset < 0, offset is {}","messagePattern":"base642bytes: offset < 0, offset is (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":618,"sourceCode":"     * @param code base64 code(0-63 is base64 char,64 is pad char).\n     * @return byte array.\n     */\n    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.\");","sourceCodeStart":600,"sourceCodeEnd":636,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L600-L636","documentation":"Thrown by the String-alphabet Base64 decoder Bytes.base642bytes(String, int off, int len, String code) when the offset `off` is negative. The decoder reads str.charAt(off + ...) so a negative offset would produce malformed indexes; it fails fast with IndexOutOfBoundsException. The same guard exists in the char[]-alphabet overload.","triggerScenarios":"Calling base642bytes(str, off, len, code) with off < 0; an offset derived from a search that returned -1 (not found) and was used unchecked as a position.","commonSituations":"Using String.indexOf result directly as the offset without checking for -1; an offset field defaulting to -1; arithmetic like off = pos - len where pos < len.","solutions":["Check the producer of the offset, especially indexOf/lastIndexOf results: if (idx < 0) handle not-found.","Prefer base642bytes(str) or base642bytes(str, code) which start at offset 0.","Clamp/validate: if (off < 0) throw new IllegalArgumentException(...)."],"exampleFix":"// before\nint idx = str.indexOf(separator);\nbyte[] b = Bytes.base642bytes(str, idx, len, C64); // idx == -1 when not found\n// after\nint idx = str.indexOf(separator);\nif (idx < 0) throw new IllegalArgumentException(\"separator not found\");\nbyte[] b = Bytes.base642bytes(str, idx, len, C64);","handlingStrategy":"validation","validationCode":"if (off < 0) throw new IllegalArgumentException(\"off must be >= 0, got \" + off);\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 offset\", e);\n}","preventionTips":["Always check indexOf/lastIndexOf for -1 before using the result as an offset.","Prefer base642bytes(str) when you mean the whole string.","Treat -1 as 'not found', never as a position."],"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"}