{"record":{"id":"9e44e6e412f35cc2","repo":"apache/dubbo","slug":"hex2bytes-len-1-1","errorCode":null,"errorMessage":"hex2bytes: ( len & 1 ) == 1.","messagePattern":"hex2bytes: \\( len & 1 \\) == 1\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":439,"sourceCode":"     *\n     * @param str hex string.\n     * @return byte array.\n     */\n    public static byte[] hex2bytes(String str) {\n        return hex2bytes(str, 0, str.length());\n    }\n\n    /**\n     * from hex string.\n     *\n     * @param str hex string.\n     * @param off offset.\n     * @param len length.\n     * @return byte array.\n     */\n    public static byte[] hex2bytes(final String str, final int off, int len) {\n        if ((len & 1) == 1) {\n            throw new IllegalArgumentException(\"hex2bytes: ( len & 1 ) == 1.\");\n        }\n\n        if (off < 0) {\n            throw new IndexOutOfBoundsException(\"hex2bytes: offset < 0, offset is \" + off);\n        }\n        if (len < 0) {\n            throw new IndexOutOfBoundsException(\"hex2bytes: length < 0, length is \" + len);\n        }\n        if (off + len > str.length()) {\n            throw new IndexOutOfBoundsException(\"hex2bytes: offset + length > array length.\");\n        }\n\n        int num = len / 2, r = off, w = 0;\n        byte[] b = new byte[num];\n        for (int i = 0; i < num; i++) {\n            b[w++] = (byte) (hex(str.charAt(r++)) << 4 | hex(str.charAt(r++)));\n        }\n        return b;","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L421-L457","documentation":"Thrown by Bytes.hex2bytes(String, int, int) when the length argument is odd (len & 1 == 1). Hex encoding uses two characters per byte, so a valid hex substring must have an even character count. An odd length indicates a malformed/truncated hex string and is rejected as IllegalArgumentException before the conversion loop.","triggerScenarios":"Calling Bytes.hex2bytes(str, off, len) where len is odd. The input hex string region must contain complete byte pairs; an odd length means a nibble is missing. This is the first validation in hex2bytes and is an argument (not index) error.","commonSituations":"Truncated hex string from a protocol field or log; a copy that dropped the last character; concatenation that produced an odd total; user input hex that was not length-validated.","solutions":["Validate the hex string length is even before calling hex2bytes, or pad/strip to an even length.","Check the source of the hex string for truncation or corruption.","Use hex2bytes(String) for the whole string and ensure the full string is even-length."],"exampleFix":"// before\nString hex = \"abc\"; // odd length\nbyte[] b = Bytes.hex2bytes(hex); // throws [154]\n\n// after\nif (hex.length() % 2 != 0) throw new IllegalArgumentException(\"odd hex length: \" + hex);\nbyte[] b = Bytes.hex2bytes(hex);","handlingStrategy":"validation","validationCode":"if (len % 2 != 0) throw new IllegalArgumentException(\"odd hex length: \" + len);\nbyte[] b = Bytes.hex2bytes(str, off, len);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate hex strings have even character count before decoding.","Use hex2bytes(String) for the whole string after an even-length check.","Reject or pad truncated hex input at the parse boundary."],"tags":["bytes","hex","validation","format"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}