{"record":{"id":"3366edae3375f8b0","repo":"Tencent/tinker","slug":"bad-dex-patch-file-magic","errorCode":null,"errorMessage":"bad dex patch file magic: ","messagePattern":"bad dex patch file magic: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"critical","filePath":"tinker-commons/src/main/java/com/tencent/tinker/commons/dexpatcher/struct/DexPatchFile.java","lineNumber":81,"sourceCode":"    private int patchedAnnotationSectionOffset;\n    private int patchedEncodedArraySectionOffset;\n    private int patchedAnnotationsDirectorySectionOffset;\n    private byte[] oldDexSignature;\n\n    public DexPatchFile(File file) throws IOException {\n        this.buffer = new DexDataBuffer(ByteBuffer.wrap(FileUtils.readFile(file)));\n        init();\n    }\n\n    public DexPatchFile(InputStream is) throws IOException {\n        this.buffer = new DexDataBuffer(ByteBuffer.wrap(FileUtils.readStream(is)));\n        init();\n    }\n\n    private void init() {\n        byte[] magic = this.buffer.readByteArray(MAGIC.length);\n        if (CompareUtils.uArrCompare(magic, MAGIC) != 0) {\n            throw new IllegalStateException(\"bad dex patch file magic: \" + Arrays.toString(magic));\n        }\n\n        this.version = this.buffer.readShort();\n        if (this.version != VERSION_02 && this.version != VERSION_03) {\n            throw new IllegalStateException(\"bad dex patch file version: \" + this.version);\n        }\n\n        if (this.version > VERSION_02) {\n            this.oldDexAPI = this.buffer.readInt();\n            this.patchedDexAPI = this.buffer.readInt();\n        }\n        this.patchedDexSize = this.buffer.readInt();\n        this.firstChunkOffset = this.buffer.readInt();\n        this.patchedStringIdSectionOffset = this.buffer.readInt();\n        this.patchedTypeIdSectionOffset = this.buffer.readInt();\n        this.patchedProtoIdSectionOffset = this.buffer.readInt();\n        this.patchedFieldIdSectionOffset = this.buffer.readInt();\n        this.patchedMethodIdSectionOffset = this.buffer.readInt();","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/Tencent/tinker/blob/1b7ea02c239840f563ea64fb5bd286eb98d4011e/tinker-commons/src/main/java/com/tencent/tinker/commons/dexpatcher/struct/DexPatchFile.java#L63-L99","documentation":"IllegalStateException from DexPatchFile.init: the first bytes of the patch file do not equal the expected 'dexpatch' magic. The stream/file being parsed is not a Tinker dex patch file at all.","triggerScenarios":"Constructing DexPatchFile(File/InputStream) over content that is not a dex patch — e.g., a full dex, a zip entry of the wrong name, an HTML error page from a download endpoint, or a file read from offset 0 of the wrong resource.","commonSituations":"Patch server returning an error page instead of the patch; extracting the wrong entry from the patch zip; passing the old/new dex where the .dex patch was expected.","solutions":["Check the downloaded payload's content type and size before parsing; the printed magic bytes reveal what was actually received","Extract the correctly-named patch entry (e.g., classes.dex patch entries) from the package","Fix the patch download URL/delivery pipeline"],"exampleFix":"// before\nnew DexPatchFile(new FileInputStream(downloadedFile));\n\n// after\nbyte[] head = new byte[8];\ntry (InputStream in = new FileInputStream(downloadedFile)) { in.read(head); }\nif (!new String(head, \"US-ASCII\").startsWith(\"dexpatch\")) throw new IOException(\"not a dex patch payload\");\nnew DexPatchFile(new FileInputStream(downloadedFile));","handlingStrategy":"validation","validationCode":"byte[] magic = new byte[8];\ntry (InputStream is = new BufferedInputStream(new FileInputStream(file))) {\n    if (is.read(magic) != magic.length || !\"dexpatch\".equals(new String(magic, 0, 8, \"US-ASCII\").trim())) {\n        throw new IOException(\"payload is not a dex patch file\");\n    }\n}\nDexPatchFile pf = new DexPatchFile(new FileInputStream(file));","typeGuard":null,"tryCatchPattern":"try {\n    new DexPatchFile(stream);\n} catch (IllegalStateException e) {\n    if (String.valueOf(e.getMessage()).startsWith(\"bad dex patch file magic\")) {\n        // log received bytes, fix download URL / extraction, do not parse further\n    } else { throw e; }\n}","preventionTips":["Check Content-Type and length of downloaded patch responses before persisting them","Extract patch entries by their exact names from the patch archive"],"tags":["dexpatcher","magic-mismatch","corrupted-patch","validation"],"backgroundTag":null,"analyzedSha":"1b7ea02c239840f563ea64fb5bd286eb98d4011e","analyzedAt":"2026-08-14T15:16:52.110Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}