Skip to content

Commit a0a6ce4

Browse files
authored
Addressing issue #781 (#782)
* Pose the problem * Quick and v dirty fix * A better and more targeted fix (to fix the previous fix)
1 parent 2f1077d commit a0a6ce4

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

Mono.Cecil.Cil/MethodBody.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void UpdateLocalScopes (Instruction removedInstruction, Instruction existingInst
312312
if (debug_info == null)
313313
return;
314314

315-
// Local scopes store start/end pair of "instruction offsets". Instruction offset can be either resolved, in which case it
315+
// Local scopes store start/end pair of "instruction offsets". Instruction offset can be either resolved, in which case it
316316
// has a reference to Instruction, or unresolved in which case it stores numerical offset (instruction offset in the body).
317317
// Typically local scopes loaded from PE/PDB files will be resolved, but it's not a requirement.
318318
// Each instruction has its own offset, which is populated on load, but never updated (this would be pretty expensive to do).
@@ -407,7 +407,7 @@ InstructionOffset ResolveInstructionOffset(InstructionOffset inputOffset, ref In
407407
// Allow for trailing null values in the case of
408408
// instructions.Size < instructions.Capacity
409409
if (item == null)
410-
break;
410+
return new InstructionOffset (items [i - 1]);
411411

412412
cache.Instruction = item;
413413

@@ -424,4 +424,4 @@ InstructionOffset ResolveInstructionOffset(InstructionOffset inputOffset, ref In
424424
}
425425
}
426426
}
427-
}
427+
}

Test/Mono.Cecil.Tests/ILProcessorTests.cs

+39
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,45 @@ public void InsertBeforeIssue697 ()
6363
}
6464
}
6565

66+
[Test]
67+
public void InsertBeforeIssue697bis ()
68+
{
69+
var parameters = new ReaderParameters { SymbolReaderProvider = new MdbReaderProvider () };
70+
using (var module = GetResourceModule ("Issue697.dll", parameters)) {
71+
var pathGetterDef = module.GetTypes ()
72+
.SelectMany (t => t.Methods)
73+
.First (m => m.Name.Equals ("get_Defer"));
74+
75+
var body = pathGetterDef.Body;
76+
var worker = body.GetILProcessor ();
77+
var initialBody = body.Instructions.ToList ();
78+
Console.WriteLine (initialBody.Sum (i => i.GetSize ()));
79+
80+
var head = initialBody.First ();
81+
var opcode = worker.Create (OpCodes.Ldc_I4_1);
82+
worker.InsertBefore (head, opcode);
83+
84+
Assert.That (pathGetterDef.DebugInformation.Scope.Start.IsEndOfMethod, Is.False);
85+
foreach (var subscope in pathGetterDef.DebugInformation.Scope.Scopes)
86+
Assert.That (subscope.Start.IsEndOfMethod, Is.False);
87+
88+
// big test -- we can write w/o crashing
89+
var unique = Guid.NewGuid ().ToString ();
90+
var output = Path.GetTempFileName ();
91+
var outputdll = output + ".dll";
92+
93+
var writer = new WriterParameters () {
94+
SymbolWriterProvider = new MdbWriterProvider (),
95+
WriteSymbols = true
96+
};
97+
using (var sink = File.Open (outputdll, FileMode.Create, FileAccess.ReadWrite)) {
98+
module.Write (sink, writer);
99+
}
100+
101+
Assert.Pass ();
102+
}
103+
}
104+
66105
[Test]
67106
public void InsertAfter ()
68107
{

0 commit comments

Comments
 (0)