From c6572aa75ec0bc10a91ae905bb3b95cff88a74ce Mon Sep 17 00:00:00 2001 From: de4dot Date: Wed, 28 Dec 2011 13:21:21 +0100 Subject: [PATCH] Allow invalid (null) targets --- blocks/CodeGenerator.cs | 2 +- blocks/InstructionListParser.cs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/blocks/CodeGenerator.cs b/blocks/CodeGenerator.cs index cd4ff32c..bb440be6 100644 --- a/blocks/CodeGenerator.cs +++ b/blocks/CodeGenerator.cs @@ -137,7 +137,7 @@ namespace de4dot.blocks { if (getShortBranch(instruction, out opcode)) { const int instrSize = 5; // It's a long branch instruction var target = (Instruction)instruction.Operand; - int distance = target.Offset - (instruction.Offset + instrSize); + int distance = target == null ? int.MaxValue : target.Offset - (instruction.Offset + instrSize); if (-0x80 <= distance && distance <= 0x7F) { instruction.OpCode = opcode; changed = true; diff --git a/blocks/InstructionListParser.cs b/blocks/InstructionListParser.cs index 7ff72578..d413a349 100644 --- a/blocks/InstructionListParser.cs +++ b/blocks/InstructionListParser.cs @@ -86,8 +86,12 @@ namespace de4dot.blocks { case OperandType.InlineSwitch: var switchTargets = (Instruction[])instr.Operand; targets = new List(switchTargets.Length); - for (int j = 0; j < switchTargets.Length; j++) - targets.Add(instrToIndex[switchTargets[j]]); + for (int j = 0; j < switchTargets.Length; j++) { + var target = switchTargets[j]; + if (target == null) + continue; + targets.Add(instrToIndex[target]); + } break; default: @@ -149,8 +153,10 @@ namespace de4dot.blocks { var switchTargets = (Instruction[])lastInstr.Operand; var newSwitchTargets = new List(); block.Targets = newSwitchTargets; - foreach (var target in switchTargets) - newSwitchTargets.Add(instrToBlock[instrToIndex[target]]); + foreach (var target in switchTargets) { + if (target != null) + newSwitchTargets.Add(instrToBlock[instrToIndex[target]]); + } break; }