From 7cb8e6534d5fc2d4eaf4b49e804d7abe55fd2543 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 3 Dec 2022 19:32:26 +0100 Subject: [PATCH] Fix #2850: Make sure that type names longer than 255 characters are handled correctly by WholeProjectDecompiler.GetFileNameForHandle --- .../WholeProjectDecompiler.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs index 70513583d..4dd22ee8f 100644 --- a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs @@ -234,7 +234,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler string GetFileFileNameForHandle(TypeDefinitionHandle 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); if (string.IsNullOrEmpty(ns)) { @@ -616,10 +616,8 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler /// static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName) { + // Remove anything that could be confused with a rooted path. int pos = text.IndexOf(':'); - if (pos > 0) - text = text.Substring(0, pos); - pos = text.IndexOf('`'); if (pos > 0) text = text.Substring(0, pos); 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: StringBuilder b = new StringBuilder(text.Length + (extension?.Length ?? 0)); foreach (var c in text) @@ -691,7 +695,15 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler b.Append('-'); string name = b.ToString(); 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; + } + if (IsReservedFileSystemName(name)) return name + "_"; else if (name == ".")