Browse Source

Fix #1907: Discard parameter names containing non-printable or white space characters from set of possible names considered by AssignVariableNames.

pull/1914/head
Siegfried Pammer 6 years ago
parent
commit
92e1d6fe85
  1. 15
      ICSharpCode.Decompiler/CSharp/OutputVisitor/TextWriterTokenWriter.cs
  2. 7
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

15
ICSharpCode.Decompiler/CSharp/OutputVisitor/TextWriterTokenWriter.cs

@ -477,6 +477,21 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
return sb.ToString(); 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) static bool IsPrintableIdentifierChar(string identifier, int index)
{ {
switch (identifier[index]) { switch (identifier[index]) {

7
ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

@ -23,6 +23,7 @@ using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Humanizer; using Humanizer;
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using ICSharpCode.Decompiler.IL; using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.Semantics; using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
@ -421,7 +422,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// remove the 'I' for interfaces // remove the 'I' for interfaces
if (name.Length >= 3 && name[0] == 'I' && char.IsUpper(name[1]) && char.IsLower(name[2])) if (name.Length >= 3 && name[0] == 'I' && char.IsUpper(name[1]) && char.IsLower(name[2]))
name = name.Substring(1); name = name.Substring(1);
name = CleanUpVariableName(name); name = CleanUpVariableName(name) ?? "obj";
} }
return name; return name;
} }
@ -466,6 +467,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms
else if (name.Length > 1 && name[0] == '_' && (char.IsLetter(name[1]) || name[1] == '_')) else if (name.Length > 1 && name[0] == '_' && (char.IsLetter(name[1]) || name[1] == '_'))
name = name.Substring(1); name = name.Substring(1);
if (TextWriterTokenWriter.ContainsNonPrintableIdentifierChar(name)) {
return null;
}
if (name.Length == 0) if (name.Length == 0)
return "obj"; return "obj";
else else

Loading…
Cancel
Save