From 5c61f7618f4e2b52b55f33c0e64afb40f2a950ff Mon Sep 17 00:00:00 2001 From: de4dot Date: Wed, 11 Apr 2012 02:00:44 +0200 Subject: [PATCH] Support invalid try handlers where start == end --- blocks/CodeGenerator.cs | 20 ++++++++++++++------ blocks/InstructionListParser.cs | 3 +++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/blocks/CodeGenerator.cs b/blocks/CodeGenerator.cs index 4546072a..1db3d945 100644 --- a/blocks/CodeGenerator.cs +++ b/blocks/CodeGenerator.cs @@ -50,7 +50,7 @@ namespace de4dot.blocks { public ExceptionInfo(int tryStart, int tryEnd, int filterStart, int handlerStart, int handlerEnd, TypeReference catchType, ExceptionHandlerType handlerType) { - if (tryStart > tryEnd || filterStart > handlerStart || handlerStart > handlerEnd || + if (tryStart > tryEnd || filterStart > handlerStart || tryStart < 0 || tryEnd < 0 || filterStart < 0 || handlerStart < 0 || handlerEnd < 0) throw new ApplicationException("Invalid start/end/filter/handler indexes"); this.tryStart = tryStart; @@ -196,11 +196,11 @@ namespace de4dot.blocks { } foreach (var ex in exceptions) { - var tryStart = blockInfos[ex.tryStart].start; - var tryEnd = blockInfos[ex.tryEnd].end; - var filterStart = ex.filterStart == -1 ? -1 : blockInfos[ex.filterStart].start; - var handlerStart = blockInfos[ex.handlerStart].start; - var handlerEnd = blockInfos[ex.handlerEnd].end; + var tryStart = getBlockInfo(blockInfos, ex.tryStart).start; + var tryEnd = getBlockInfo(blockInfos, ex.tryEnd).end; + var filterStart = ex.filterStart == -1 ? -1 : getBlockInfo(blockInfos, ex.filterStart).start; + var handlerStart = getBlockInfo(blockInfos, ex.handlerStart).start; + var handlerEnd = getBlockInfo(blockInfos, ex.handlerEnd).end; var eh = new ExceptionHandler(ex.handlerType); eh.CatchType = ex.catchType; @@ -214,6 +214,14 @@ namespace de4dot.blocks { } } + static BlockInfo getBlockInfo(List blockInfos, int index) { + if (index >= blockInfos.Count) + index = blockInfos.Count - 1; + if (index < 0) + index = 0; + return blockInfos[index]; + } + static Instruction getInstruction(IList allInstructions, int i) { if (i < allInstructions.Count) return allInstructions[i]; diff --git a/blocks/InstructionListParser.cs b/blocks/InstructionListParser.cs index f7b3b83d..33288f03 100644 --- a/blocks/InstructionListParser.cs +++ b/blocks/InstructionListParser.cs @@ -289,6 +289,9 @@ namespace de4dot.blocks { // Replace the BaseBlocks with a new BaseBlock, returning the old ones. public List replace(int startInstr, int endInstr, ScopeBlock bb) { + if (endInstr < startInstr) + return new List(); + int startIndex, endIndex; var rv = getBlocks(startInstr, endInstr, out startIndex, out endIndex); updateParent(rv, bb);