{"record":{"id":"a9ef2971af56f801","repo":"pxb1988/dex2jar","slug":"bad-byte","errorCode":null,"errorMessage":"bad byte","messagePattern":"bad byte","errorType":"exception","errorClass":"UTFDataFormatException","httpStatus":null,"severity":"error","filePath":"dex-reader/src/main/java/com/googlecode/d2j/util/Mutf8.java","lineNumber":59,"sourceCode":"            }\n\n            if (a < '\\u0080') {\n                sb.append(a);\n            } else if ((a & 0xe0) == 0xc0) {\n                int b = in.get() & 0xff;\n                if ((b & 0xC0) != 0x80) {\n                    throw new UTFDataFormatException(\"bad second byte\");\n                }\n                sb.append((char) (((a & 0x1F) << 6) | (b & 0x3F)));\n            } else if ((a & 0xf0) == 0xe0) {\n                int b = in.get() & 0xff;\n                int c = in.get() & 0xff;\n                if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {\n                    throw new UTFDataFormatException(\"bad second or third byte\");\n                }\n                sb.append((char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F)));\n            } else {\n                throw new UTFDataFormatException(\"bad byte\");\n            }\n        }\n    }\n\n    /**\n     * Returns the number of bytes the modified UTF8 representation of 's' would take.\n     */\n    private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {\n        long result = 0;\n        final int length = s.length();\n        for (int i = 0; i < length; ++i) {\n            char ch = s.charAt(i);\n            if (ch != 0 && ch <= 127) { // U+0000 uses two bytes.\n                ++result;\n            } else if (ch <= 2047) {\n                result += 2;\n            } else {\n                result += 3;","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/pxb1988/dex2jar/blob/b5bda4fb4935ae8b3869b422454ae3b3896c7bc1/dex-reader/src/main/java/com/googlecode/d2j/util/Mutf8.java#L41-L77","documentation":"Thrown by Mutf8.decode when a lead byte is outside the valid MUTF-8 ranges: it is >= 0xF0 (including 4-byte UTF-8 sequences, which Modified UTF-8 does not support) or a byte with pattern 10xx xxxx appearing as a lead byte.","triggerScenarios":"Decoding bytes containing standard UTF-8 4-byte sequences (e.g. emoji, supplementary characters encoded as surrogate pairs outside Java) or continuation bytes where a lead byte was expected.","commonSituations":"Strings encoded with standard UTF-8 instead of Modified UTF-8 (surrogate-pair 4-byte encodings), corrupted input, misaligned buffer offsets.","solutions":["Ensure producers encode with Modified UTF-8 (e.g. DataOutputStream.writeUTF semantics), where supplementary chars are 6-byte surrogate pairs, not 4-byte UTF-8","Re-encode the input string via Mutf8.encode / utf8Bytes before decoding","Catch UTFDataFormatException and log the offending offset; treat the DEX as invalid"],"exampleFix":"// before\nbyte[] raw = standardUtf8String.getBytes(StandardCharsets.UTF_8);\n// after\nbyte[] raw = Mutf8.encode(s); // MUTF-8: surrogates as two 3-byte sequences","handlingStrategy":"validation","validationCode":"for (byte x : bytes) { int a = x & 0xff; if (a >= 0xF0) return false; } // reject 4-byte UTF-8 leads before decode","typeGuard":"boolean isMutf8Compatible(byte[] bytes) { return java.util.Arrays.stream(bytes).noneMatch(b -> (b & 0xff) >= 0xF0); }","tryCatchPattern":"try { return Mutf8.decode(buf, pos); } catch (UTFDataFormatException e) { log.error(\"non-MUTF-8 byte at \" + pos[0]); return new String(rawFallback, StandardCharsets.UTF_8); }","preventionTips":["Encode with Mutf8.encode / DataOutputStream.writeUTF, never raw UTF-8 bytes","Remember MUTF-8 has no 4-byte sequences: supplementary chars must go through surrogate pairs","Reject or transcode external UTF-8 data before feeding it into MUTF-8 decoders"],"tags":["utf-8","dex","decoding","encoding"],"backgroundTag":"invalid-argument-format","analyzedSha":"b5bda4fb4935ae8b3869b422454ae3b3896c7bc1","analyzedAt":"2026-09-08T00:44:01.258Z","contentChangedAt":"2026-09-08T00:44:01.258Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}