{"record":{"id":"dd38bc3412a1613a","repo":"Tencent/tinker","slug":"bad-patch-operation-sequence-addcounter-d-addc","errorCode":null,"errorMessage":"bad patch operation sequence. addCounter: %d, addCount: %d, delCounter: %d, delCount: %d, replaceCounter: %d, replaceCount:%d","messagePattern":"bad patch operation sequence\\. addCounter: (.+?), addCount: (.+?), delCounter: (.+?), delCount: (.+?), replaceCounter: (.+?), replaceCount:(.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"critical","filePath":"tinker-commons/src/main/java/com/tencent/tinker/commons/dexpatcher/algorithms/patch/DexSectionPatchAlgorithm.java","lineNumber":229,"sourceCode":"                int patchedOffset = writePatchedItem(oldItem);\n\n                updateIndexOrOffset(\n                        this.oldToPatchedIndexMap,\n                        oldIndex,\n                        getItemOffsetOrIndex(oldIndex, oldItem),\n                        patchedIndex,\n                        patchedOffset\n                );\n\n                ++oldIndex;\n                ++patchedIndex;\n            }\n        }\n\n        if (addActionCursor != addedItemCount || deletedItemCounter != deletedItemCount\n                || replaceActionCursor != replacedItemCount\n        ) {\n            throw new IllegalStateException(\n                    String.format(\n                            \"bad patch operation sequence. addCounter: %d, addCount: %d, \"\n                                    + \"delCounter: %d, delCount: %d, \"\n                                    + \"replaceCounter: %d, replaceCount:%d\",\n                            addActionCursor,\n                            addedItemCount,\n                            deletedItemCounter,\n                            deletedItemCount,\n                            replaceActionCursor,\n                            replacedItemCount\n                    )\n            );\n        }\n\n        onPatchAlgorithmEnd();\n    }\n}\n","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/Tencent/tinker/blob/1b7ea02c239840f563ea64fb5bd286eb98d4011e/tinker-commons/src/main/java/com/tencent/tinker/commons/dexpatcher/algorithms/patch/DexSectionPatchAlgorithm.java#L211-L247","documentation":"IllegalStateException from DexSectionPatchAlgorithm's patch-application loop: after walking old and patched item lists, the executed number of add/delete/replace operations does not match the counts recorded in the patch file header. The operation stream and the item cursors desynchronized, indicating a corrupt or incompatible patch file.","triggerScenarios":"Applying a dex section patch where the add/del/replace counters in DexSectionPatchAlgorithm's run loop end up unequal to addedItemCount/deletedItemCount/replacedItemCount from the patch — e.g., truncated patch data or generator/applier version mismatch.","commonSituations":"Patch file corrupted in transit or by partial download; generating patches with one Tinker version and applying with another (different algorithm versions); tampered patch zips.","solutions":["Verify the patch package's MD5/SHA-1 (Tinker patch digest) before applying; re-download if mismatched","Use the same Tinker version in the gradle plugin and the on-device loader","Regenerate the patch with the current toolchain"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify whole patch package digest before applying any dex patch\nif (!Arrays.equals(expectedMd5, SharePatchFileUtil.getMD5(patchFile))) {\n    throw new IOException(\"patch digest mismatch; redownload\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    applier.executeAndSaveTo(out);\n} catch (IllegalStateException e) {\n    if (String.valueOf(e.getMessage()).startsWith(\"bad patch operation sequence\")) {\n        // corrupt or incompatible patch: redownload / regenerate, never retry as-is\n    } else { throw e; }\n}","preventionTips":["Always verify patch MD5/SHA before application (Tinker does this at package level; keep it enabled)","Pin identical Tinker plugin and runtime versions across your release pipeline"],"tags":["dexpatcher","corrupted-patch","counter-mismatch","patch-apply"],"backgroundTag":null,"analyzedSha":"1b7ea02c239840f563ea64fb5bd286eb98d4011e","analyzedAt":"2026-08-14T15:16:52.110Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}