From 9becae2de7658420736d24c963c0d12546c1e31f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 11 Aug 2023 17:26:22 +0200 Subject: [PATCH] Fix #3051: "InvalidOperationException: Stack empty" when decompiling reference assembly with empty method bodies --- ICSharpCode.Decompiler/IL/BlockBuilder.cs | 2 +- ICSharpCode.Decompiler/IL/ILReader.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/IL/BlockBuilder.cs b/ICSharpCode.Decompiler/IL/BlockBuilder.cs index c21881673..7135a6129 100644 --- a/ICSharpCode.Decompiler/IL/BlockBuilder.cs +++ b/ICSharpCode.Decompiler/IL/BlockBuilder.cs @@ -136,7 +136,7 @@ namespace ICSharpCode.Decompiler.IL cancellationToken.ThrowIfCancellationRequested(); int start = block.StartILOffset; // Leave nested containers if necessary - while (start >= currentContainer.EndILOffset) + while (start >= currentContainer.EndILOffset && containerStack.Count > 0) { currentContainer = containerStack.Pop(); } diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index 901076a22..10046abaf 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -462,6 +462,7 @@ namespace ICSharpCode.Decompiler.IL blocksByOffset[0].Block.Instructions.Add( new InvalidBranch("Empty body found. Decompiled assembly might be a reference assembly.") ); + stackVariables = Enumerable.Empty(); return; } ILParser.SetBranchTargets(ref reader, isBranchTarget);