Skip to content

Commit 40c55c1

Browse files
committed
Fixed explicitly applied maxstack being ignored.
1 parent 870ce3e commit 40c55c1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Mono.Cecil.Cil/CodeWriter.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ void ComputeHeader ()
341341
}
342342

343343
body.code_size = offset;
344-
body.max_stack_size = max_stack;
344+
if (body.max_stack_size == 0) {
345+
body.max_stack_size = max_stack;
346+
}
345347
}
346348

347349
void ComputeExceptionHandlerStackSize (ref Dictionary<Instruction, int> stack_sizes)

Test/Mono.Cecil.Tests/MethodBodyTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34

45
using Mono.Cecil;
@@ -449,5 +450,31 @@ public void RemoveInstruction ()
449450
Assert.AreEqual (first, third.Previous);
450451
Assert.IsNull (third.Next);
451452
}
453+
454+
[Test]
455+
public void ApplyExplicitMaxStack ()
456+
{
457+
var path = Path.GetTempFileName ();
458+
var module = ModuleDefinition.CreateModule ("FooFoo", ModuleKind.Dll);
459+
460+
var method = new MethodDefinition ("foo", MethodAttributes.Static, module.TypeSystem.Void);
461+
var body = method.Body;
462+
463+
body.MaxStackSize = 100;
464+
465+
var il = body.GetILProcessor ();
466+
467+
var ret = il.Create (OpCodes.Ret);
468+
body.Instructions.Add (ret);
469+
470+
var type = new TypeDefinition ("foo", "foo", TypeAttributes.Public | TypeAttributes.Class, module.TypeSystem.Object);
471+
type.Methods.Add (method);
472+
module.Types.Add (type);
473+
474+
module.Write (path);
475+
476+
using (var read_module = ModuleDefinition.ReadModule (path))
477+
Assert.AreEqual (100, read_module.Types [1].Methods[0].Body.MaxStackSize);
478+
}
452479
}
453480
}

0 commit comments

Comments
 (0)