{"record":{"id":"015e9f32a1cb7fa4","repo":"skylot/jadx","slug":"unexpected-registers-count-in","errorCode":null,"errorMessage":"Unexpected registers count in {}","messagePattern":"Unexpected registers count in (.+?)","errorType":"exception","errorClass":"JadxRuntimeException","httpStatus":null,"severity":"error","filePath":"jadx-core/src/main/java/jadx/core/dex/instructions/ArithNode.java","lineNumber":26,"sourceCode":"import jadx.core.dex.instructions.args.InsnArg;\nimport jadx.core.dex.instructions.args.LiteralArg;\nimport jadx.core.dex.instructions.args.RegisterArg;\nimport jadx.core.dex.nodes.InsnNode;\nimport jadx.core.utils.InsnUtils;\nimport jadx.core.utils.exceptions.JadxRuntimeException;\n\npublic class ArithNode extends InsnNode {\n\n\tpublic static ArithNode build(InsnData insn, ArithOp op, ArgType type) {\n\t\tRegisterArg resArg = InsnArg.reg(insn, 0, fixResultType(op, type));\n\t\tArgType argType = fixArgType(op, type);\n\t\tswitch (insn.getRegsCount()) {\n\t\t\tcase 2:\n\t\t\t\treturn new ArithNode(op, resArg, InsnArg.reg(insn, 0, argType), InsnArg.reg(insn, 1, argType));\n\t\t\tcase 3:\n\t\t\t\treturn new ArithNode(op, resArg, InsnArg.reg(insn, 1, argType), InsnArg.reg(insn, 2, argType));\n\t\t\tdefault:\n\t\t\t\tthrow new JadxRuntimeException(\"Unexpected registers count in \" + insn);\n\t\t}\n\t}\n\n\tpublic static ArithNode buildLit(InsnData insn, ArithOp op, ArgType type) {\n\t\tRegisterArg resArg = InsnArg.reg(insn, 0, fixResultType(op, type));\n\t\tArgType argType = fixArgType(op, type);\n\t\tLiteralArg litArg = InsnArg.lit(insn, argType);\n\t\tswitch (insn.getRegsCount()) {\n\t\t\tcase 1:\n\t\t\t\treturn new ArithNode(op, resArg, InsnArg.reg(insn, 0, argType), litArg);\n\t\t\tcase 2:\n\t\t\t\treturn new ArithNode(op, resArg, InsnArg.reg(insn, 1, argType), litArg);\n\t\t\tdefault:\n\t\t\t\tthrow new JadxRuntimeException(\"Unexpected registers count in \" + insn);\n\t\t}\n\t}\n\n\tprivate static ArgType fixResultType(ArithOp op, ArgType type) {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/skylot/jadx/blob/e738a26571d02919f01df40de93bc9a44dee4e18/jadx-core/src/main/java/jadx/core/dex/instructions/ArithNode.java#L8-L44","documentation":"Thrown by ArithNode.build when decoding a binary arithmetic instruction (e.g. add/sub/mul) whose register count is neither 2 (two-register form: v0 = v0 op v1) nor 3 (three-register form: v0 = v1 op v2). DEX arithmetic opcodes are defined with exactly these register layouts; any other count is malformed.","triggerScenarios":"Decoding a Dalvik arithmetic instruction whose regsCount is 0, 1, or >3. Indicates a corrupt DEX, an instruction that was incorrectly decoded (wrong opcode/unit size), or a manually-crafted instruction stream.","commonSituations":"Corrupted/truncated DEX files; obfuscators that emit non-standard instruction encodings; bugs in the instruction reader that miscompute register count; mismatched DexBackedInstruction versions.","solutions":["Validate the DEX is well-formed (run a verifier like dexdump / baksmali) before decompiling.","Log insn (printed in the message) to see the opcode and decoded registers; a wrong unit-size read is the usual culprit.","Update jadx; instruction decoding has many version-specific fixes.","Report the malformed DEX; if unavoidable, catch JadxRuntimeException at the method-decode boundary to skip the method."],"exampleFix":"// before\nswitch (insn.getRegsCount()) {\n    case 2: return new ArithNode(op, resArg, reg(0), reg(1));\n    case 3: return new ArithNode(op, resArg, reg(1), reg(2));\n    default: throw new JadxRuntimeException(\"Unexpected registers count in \" + insn);\n}\n\n// after (decode boundary guard)\ntry {\n    return ArithNode.build(insn, op, type);\n} catch (JadxRuntimeException e) {\n    LOG.warn(\"Skipping malformed arith insn in {}: {}\", mth, insn);\n    mth.add(AFlag.INCONSISTENT_CODE);\n    return null;\n}","handlingStrategy":"try-catch","validationCode":"int rc = insn.getRegsCount();\nif (rc != 2 && rc != 3) {\n    LOG.warn(\"Malformed arith instruction with regsCount={} in {}\", rc, mth);\n    mth.add(AFlag.INCONSISTENT_CODE);\n}","typeGuard":"static boolean isValidArithRegCount(int rc) {\n    return rc == 2 || rc == 3;\n}","tryCatchPattern":"try {\n    insn = ArithNode.build(insn, op, type);\n} catch (JadxRuntimeException e) {\n    LOG.warn(\"Bad arith decode in {}: {}\", mth, insn);\n    mth.add(AFlag.INCONSISTENT_CODE);\n    insn = null;\n}","preventionTips":["Verify DEX with baksmali/dexdump before decompiling.","Decode each instruction under a try/catch so one bad opcode does not abort the method.","Flag inconsistent methods rather than crashing on malformed inputs."],"tags":["bytecode","dex","instruction-decode","invariant"],"backgroundTag":null,"analyzedSha":"e738a26571d02919f01df40de93bc9a44dee4e18","analyzedAt":"2026-08-14T00:10:24.238Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}