{"record":{"id":"dc8713b230574b08","repo":"MuntashirAkon/AppManager","slug":"invalid-hex-length-length","errorCode":null,"errorMessage":"Invalid hex length \" + length","messagePattern":"Invalid hex length \" \\+ length","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/BinaryXmlPullParser.java","lineNumber":917,"sourceCode":"        throw new IllegalArgumentException(\"Invalid hex char '\" + c + \"'\");\n    }\n\n    static String bytesToHexString(byte[] value) {\n        final int length = value.length;\n        final char[] buf = new char[length * 2];\n        int bufIndex = 0;\n        for (int i = 0; i < length; i++) {\n            byte b = value[i];\n            buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];\n            buf[bufIndex++] = HEX_DIGITS[b & 0x0F];\n        }\n        return new String(buf);\n    }\n\n    static byte[] hexStringToBytes(String value) {\n        final int length = value.length();\n        if (length % 2 != 0) {\n            throw new IllegalArgumentException(\"Invalid hex length \" + length);\n        }\n        byte[] buffer = new byte[length / 2];\n        for (int i = 0; i < length; i += 2) {\n            buffer[i / 2] = (byte) ((toByte(value.charAt(i)) << 4)\n                    | toByte(value.charAt(i + 1)));\n        }\n        return buffer;\n    }\n}","sourceCodeStart":899,"sourceCodeEnd":926,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/BinaryXmlPullParser.java#L899-L926","documentation":"Validation guard in BinaryXmlPullParser.hexStringToBytes: a hex-encoded binary attribute string must have an even number of characters (2 chars per byte); the parser encountered a string whose length is odd, meaning the attribute value is truncated or corrupted, and refuses to decode it. Faulty input: the odd-length hex string being parsed from the binary XML.","triggerScenarios":"Passing a hex string with an odd number of characters to hexStringToBytes() or reading a TYPE_BYTES_HEX attribute whose stored text has odd length.","commonSituations":"Truncated or hand-trimmed hex values (a leading \"0 \" accidentally dropped), or a producer that serialized an odd-length string.","solutions":["Fix the producer to always emit an even number of hex chars (bytesToHexString never produces odd output).","Validate value.length() % 2 == 0 before decoding; pad with a leading '0' only if the original data allows it.","Re-export the attribute from the source data if it was truncated in storage.","Catch IllegalArgumentException and treat the attribute as corrupt."],"exampleFix":"// before\nbyte[] b = hexStringToBytes(\"abc\"); // odd length throws\n// after\nString hex = \"abc\";\nif (hex.length() % 2 != 0) hex = \"0\" + hex; // or reject\nbyte[] b = hexStringToBytes(hex);","handlingStrategy":"validation","validationCode":"boolean evenLen = value != null && value.length() % 2 == 0;","typeGuard":null,"tryCatchPattern":"try {\n    byte[] b = hexStringToBytes(value);\n} catch (IllegalArgumentException e) {\n    // treat as corrupt attribute; fall back to stored raw bytes if available\n}","preventionTips":["Validate even length before decoding.","Produce hex exclusively from bytesToHexString, which always emits pairs.","Guard against truncation when persisting hex strings (fixed widths, checksums).","For odd-length legacy data, decide explicitly whether to left-pad or reject."],"tags":["xml","hex","decoding"],"backgroundTag":"invalid-argument-format","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}