From 2c68d993365051b9eb7cca237d62ef125b350eed Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 2 Nov 2017 23:31:54 +0100 Subject: [PATCH] Fix #959: Crash in CSharp.StatementBuilder.TranslateSwitch (due to empty sequence) --- .../ICSharpCode.Decompiler.Tests.csproj | 2 ++ .../ILPrettyTestRunner.cs | 6 ++++ .../TestCases/ILPretty/Issue959.cs | 12 +++++++ .../TestCases/ILPretty/Issue959.il | 36 +++++++++++++++++++ .../CSharp/StatementBuilder.cs | 2 +- 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.cs create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.il diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index a722b7b70..e5e3b7b8f 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -59,6 +59,7 @@ + @@ -79,6 +80,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs index 05ff2be06..1ddfb26d5 100644 --- a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs @@ -42,6 +42,12 @@ namespace ICSharpCode.Decompiler.Tests Run(); } + [Test] + public void Issue959() + { + Run(); + } + [Test] public void FSharpUsing_Debug() { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.cs b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.cs new file mode 100644 index 000000000..c9fc8ee2a --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.cs @@ -0,0 +1,12 @@ +namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty +{ + internal class Issue959 + { + public void Test(bool arg) + { + switch (arg) { + + } + } + } +} diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.il b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.il new file mode 100644 index 000000000..754593da2 --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue959.il @@ -0,0 +1,36 @@ +// Metadata version: v4.0.30319 +.assembly extern mscorlib +{ + .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. + .ver 4:0:0:0 +} +.assembly ConsoleApp11 +{ + .ver 1:0:0:0 +} +.module ConsoleApp11.exe +// MVID: {B973FCD6-A9C4-48A9-8291-26DDC248E208} +.imagebase 0x00400000 +.file alignment 0x00000200 +.stackreserve 0x00100000 +.subsystem 0x0003 // WINDOWS_CUI +.corflags 0x00020003 // ILONLY 32BITPREFERRED +// Image base: 0x000001C4B6C90000 + +.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue959 + extends [mscorlib]System.Object +{ +.method public hidebysig instance void Test( + bool arg + ) cil managed +{ + // Code size 18 (0x12) + .maxstack 8 + IL_0000: ldarg.1 + IL_0001: brfalse IL_000b + IL_0006: br IL_0011 + IL_000b: ldarg.1 + IL_000c: brtrue IL_0011 + IL_0011: ret +} // end of method Program::Test +} \ No newline at end of file diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index 995b7a247..52b85f519 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -186,7 +186,7 @@ namespace ICSharpCode.Decompiler.CSharp break; } } - if (switchContainer != null) { + if (switchContainer != null && stmt.SwitchSections.Count > 0) { // Translate any remaining blocks: var lastSectionStatements = stmt.SwitchSections.Last().Statements; foreach (var block in switchContainer.Blocks.Skip(1)) {