Browse Source

Fix #2850: Make sure that type names longer than 255 characters are handled correctly by WholeProjectDecompiler.GetFileNameForHandle

pull/2857/head
Siegfried Pammer 2 years ago
parent
commit
7cb8e6534d
  1. 20
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

20
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

@ -234,7 +234,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
string GetFileFileNameForHandle(TypeDefinitionHandle h) string GetFileFileNameForHandle(TypeDefinitionHandle h)
{ {
var type = metadata.GetTypeDefinition(h); var type = metadata.GetTypeDefinition(h);
string file = CleanUpFileName(metadata.GetString(type.Name)) + ".cs"; string file = SanitizeFileName(metadata.GetString(type.Name) + ".cs");
string ns = metadata.GetString(type.Namespace); string ns = metadata.GetString(type.Namespace);
if (string.IsNullOrEmpty(ns)) if (string.IsNullOrEmpty(ns))
{ {
@ -616,10 +616,8 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
/// </summary> /// </summary>
static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName) static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName)
{ {
// Remove anything that could be confused with a rooted path.
int pos = text.IndexOf(':'); int pos = text.IndexOf(':');
if (pos > 0)
text = text.Substring(0, pos);
pos = text.IndexOf('`');
if (pos > 0) if (pos > 0)
text = text.Substring(0, pos); text = text.Substring(0, pos);
text = text.Trim(); text = text.Trim();
@ -650,6 +648,12 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
} }
} }
} }
// Remove generics
pos = text.IndexOf('`');
if (pos > 0)
{
text = text.Substring(0, pos).Trim();
}
// Whitelist allowed characters, replace everything else: // Whitelist allowed characters, replace everything else:
StringBuilder b = new StringBuilder(text.Length + (extension?.Length ?? 0)); StringBuilder b = new StringBuilder(text.Length + (extension?.Length ?? 0));
foreach (var c in text) foreach (var c in text)
@ -691,7 +695,15 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
b.Append('-'); b.Append('-');
string name = b.ToString(); string name = b.ToString();
if (extension != null) if (extension != null)
{
// make sure that adding the extension to the filename
// does not exceed maxSegmentLength.
// trim the name, if necessary.
if (name.Length + extension.Length > maxSegmentLength)
name = name.Remove(name.Length - extension.Length);
name += extension; name += extension;
}
if (IsReservedFileSystemName(name)) if (IsReservedFileSystemName(name))
return name + "_"; return name + "_";
else if (name == ".") else if (name == ".")

Loading…
Cancel
Save