From 92e1d6fe85fb59a35ffab696534270b3fa15347c Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 16 Jan 2020 20:52:25 +0100 Subject: [PATCH] Fix #1907: Discard parameter names containing non-printable or white space characters from set of possible names considered by AssignVariableNames. --- .../CSharp/OutputVisitor/TextWriterTokenWriter.cs | 15 +++++++++++++++ .../IL/Transforms/AssignVariableNames.cs | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/CSharp/OutputVisitor/TextWriterTokenWriter.cs b/ICSharpCode.Decompiler/CSharp/OutputVisitor/TextWriterTokenWriter.cs index 1c7d3ea6d..8aa81a758 100644 --- a/ICSharpCode.Decompiler/CSharp/OutputVisitor/TextWriterTokenWriter.cs +++ b/ICSharpCode.Decompiler/CSharp/OutputVisitor/TextWriterTokenWriter.cs @@ -477,6 +477,21 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor return sb.ToString(); } + public static bool ContainsNonPrintableIdentifierChar(string identifier) + { + if (string.IsNullOrEmpty(identifier)) + return false; + + for (int i = 0; i < identifier.Length; i++) { + if (char.IsWhiteSpace(identifier[i])) + return true; + if (!IsPrintableIdentifierChar(identifier, i)) + return true; + } + + return false; + } + static bool IsPrintableIdentifierChar(string identifier, int index) { switch (identifier[index]) { diff --git a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs index 61913889f..e5f28ff72 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs @@ -23,6 +23,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using Humanizer; +using ICSharpCode.Decompiler.CSharp.OutputVisitor; using ICSharpCode.Decompiler.IL; using ICSharpCode.Decompiler.Semantics; using ICSharpCode.Decompiler.TypeSystem; @@ -421,7 +422,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms // remove the 'I' for interfaces if (name.Length >= 3 && name[0] == 'I' && char.IsUpper(name[1]) && char.IsLower(name[2])) name = name.Substring(1); - name = CleanUpVariableName(name); + name = CleanUpVariableName(name) ?? "obj"; } return name; } @@ -466,6 +467,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms else if (name.Length > 1 && name[0] == '_' && (char.IsLetter(name[1]) || name[1] == '_')) name = name.Substring(1); + if (TextWriterTokenWriter.ContainsNonPrintableIdentifierChar(name)) { + return null; + } + if (name.Length == 0) return "obj"; else