apache/dubbo · error · IllegalArgumentException
hex2bytes: ( len & 1 ) == 1.
Error message
hex2bytes: ( len & 1 ) == 1.
What it means
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.
Source
Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java:439
*
* @param str hex string.
* @return byte array.
*/
public static byte[] hex2bytes(String str) {
return hex2bytes(str, 0, str.length());
}
/**
* from hex string.
*
* @param str hex string.
* @param off offset.
* @param len length.
* @return byte array.
*/
public static byte[] hex2bytes(final String str, final int off, int len) {
if ((len & 1) == 1) {
throw new IllegalArgumentException("hex2bytes: ( len & 1 ) == 1.");
}
if (off < 0) {
throw new IndexOutOfBoundsException("hex2bytes: offset < 0, offset is " + off);
}
if (len < 0) {
throw new IndexOutOfBoundsException("hex2bytes: length < 0, length is " + len);
}
if (off + len > str.length()) {
throw new IndexOutOfBoundsException("hex2bytes: offset + length > array length.");
}
int num = len / 2, r = off, w = 0;
byte[] b = new byte[num];
for (int i = 0; i < num; i++) {
b[w++] = (byte) (hex(str.charAt(r++)) << 4 | hex(str.charAt(r++)));
}
return b;View on GitHub (pinned to 3a3043227f)
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.
Example fix
// before
String hex = "abc"; // odd length
byte[] b = Bytes.hex2bytes(hex); // throws [154]
// after
if (hex.length() % 2 != 0) throw new IllegalArgumentException("odd hex length: " + hex);
byte[] b = Bytes.hex2bytes(hex); Defensive patterns
Strategy: validation
Validate before calling
if (len % 2 != 0) throw new IllegalArgumentException("odd hex length: " + len);
byte[] b = Bytes.hex2bytes(str, off, len); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- bytes2hex: offset < 0, offset is {}
- bytes2hex: length < 0, length is {}
- bytes2hex: offset + length > array length.
- hex2bytes: offset < 0, offset is {}
- hex2bytes: length < 0, length is {}
AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14).
Data as JSON: /api/errors/9e44e6e412f35cc2.
Report an issue: GitHub.