diff --git a/blocks/Blocks.cs b/blocks/Blocks.cs index 00159489..a888e9f7 100644 --- a/blocks/Blocks.cs +++ b/blocks/Blocks.cs @@ -183,8 +183,16 @@ namespace de4dot.blocks { } public void repartitionBlocks() { - foreach (var scopeBlock in getAllScopeBlocks(methodBlocks)) - scopeBlock.repartitionBlocks(); + foreach (var scopeBlock in getAllScopeBlocks(methodBlocks)) { + try { + scopeBlock.repartitionBlocks(); + } + catch (NullReferenceException) { + //TODO: Send this message to the log + Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MetadataToken.ToUInt32(), method.FullName); + return; + } + } } } } diff --git a/blocks/cflow/BlockCflowDeobfuscator.cs b/blocks/cflow/BlockCflowDeobfuscator.cs index f4811671..b9f07532 100644 --- a/blocks/cflow/BlockCflowDeobfuscator.cs +++ b/blocks/cflow/BlockCflowDeobfuscator.cs @@ -38,9 +38,15 @@ namespace de4dot.blocks.cflow { var instructions = block.Instructions; if (instructions.Count == 0) return false; - for (int i = 0; i < instructions.Count - 1; i++) { - var instr = instructions[i].Instruction; - instructionEmulator.emulate(instr); + try { + for (int i = 0; i < instructions.Count - 1; i++) { + var instr = instructions[i].Instruction; + instructionEmulator.emulate(instr); + } + } + catch (System.NullReferenceException) { + // Here if eg. invalid metadata token in a call instruction (operand is null) + return false; } switch (block.LastInstr.OpCode.Code) { diff --git a/blocks/cflow/DeadCodeRemover.cs b/blocks/cflow/DeadCodeRemover.cs index 0387c939..4634565b 100644 --- a/blocks/cflow/DeadCodeRemover.cs +++ b/blocks/cflow/DeadCodeRemover.cs @@ -35,8 +35,14 @@ namespace de4dot.blocks.cflow { public bool remove() { bool changed = false; - foreach (var block in allBlocks) - changed |= remove(block); + foreach (var block in allBlocks) { + try { + changed |= remove(block); + } + catch (System.NullReferenceException) { + // Here if eg. invalid metadata token in a call instruction (operand is null) + } + } return changed; }