From 15028b3669d43258c8f12646f2c037abfd59f231 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 23 Aug 2020 09:12:48 +0200 Subject: [PATCH] Fix #2104: Check field name of backing field when attempting to transform to an auto property. --- .../ICSharpCode.Decompiler.Tests.csproj | 2 + .../ILPrettyTestRunner.cs | 6 ++ .../TestCases/ILPretty/Issue2104.cs | 20 +++++ .../TestCases/ILPretty/Issue2104.il | 81 +++++++++++++++++++ .../Transforms/PatternStatementTransform.cs | 2 +- 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.cs create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.il diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index 93cbbe251..573074b3f 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -66,6 +66,7 @@ + @@ -94,6 +95,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs index cd864eab9..211a911dd 100644 --- a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs @@ -178,6 +178,12 @@ namespace ICSharpCode.Decompiler.Tests Run(); } + [Test] + public void Issue2104() + { + Run(); + } + [Test] public void ConstantBlobs() { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.cs b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.cs new file mode 100644 index 000000000..f4503289f --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.cs @@ -0,0 +1,20 @@ +using System.Runtime.CompilerServices; + +namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty +{ + internal class Issue2104 + { + [CompilerGenerated] + private readonly string text; + public string Text { + [CompilerGenerated] + get { + return text; + } + } + public Issue2104(string text) + { + this.text = text; + } + } +} diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.il b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.il new file mode 100644 index 000000000..4ef0c1fab --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue2104.il @@ -0,0 +1,81 @@ +.assembly issue2104 +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( + 01 00 08 00 00 00 00 00 + ) + .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( + 01 00 01 00 54 02 16 57 72 61 70 4e 6f 6e 45 78 + 63 65 70 74 69 6f 6e 54 68 72 6f 77 73 01 + ) + .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( + 01 00 07 01 00 00 00 00 + ) + .hash algorithm 0x00008004 // SHA1 + .ver 0:0:0:0 +} + +.module issue2104.exe +// MVID: {A7498FA1-F4D7-486E-A872-3DC0CAFD87E2} +.imagebase 0x10000000 +.file alignment 0x00000200 +.stackreserve 0x00100000 +.subsystem 0x0003 // WindowsCui +.corflags 0x00000001 // ILOnly +.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( + 01 00 00 00 +) + +.class private auto ansi '' +{ +} // end of class + +.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue2104 + extends [mscorlib]System.Object +{ + // Fields + .field private initonly string 'text' + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( + 01 00 00 00 + ) + + // Methods + .method public hidebysig specialname + instance string get_Text () cil managed + { + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( + 01 00 00 00 + ) + // Method begins at RVA 0x2074 + // Code size 7 (0x7) + .maxstack 8 + + IL_0000: ldarg.0 + IL_0001: ldfld string ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue2104::text + IL_0006: ret + } // end of method ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue2104::get_Text + + .method public hidebysig specialname rtspecialname + instance void .ctor ( + string text + ) cil managed + { + // Method begins at RVA 0x207c + // Code size 14 (0xe) + .maxstack 8 + + IL_0000: ldarg.0 + IL_0001: call instance void [mscorlib]System.Object::.ctor() + IL_0006: ldarg.0 + IL_0007: ldarg.1 + IL_0008: stfld string ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue2104::'text' + IL_000d: ret + } // end of method ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue2104::.ctor + + // Properties + .property instance string Text() + { + .get instance string ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue2104::get_Text() + } + +} // end of class ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue2104 + diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs index c65d02d75..338b889e2 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs @@ -586,7 +586,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms field = m2.Get("fieldReference").Single().GetSymbol() as IField; } } - if (field == null) + if (field == null || !(field.Name.StartsWith("<") && field.Name.EndsWith(">k__BackingField"))) return null; if (propertyDeclaration.Setter.HasModifier(Modifiers.Readonly)) return null;