Skip to content

System.IO.PathTooLongException on Linux because limit is 255 UTF-8 bytes #3612

@grompe

Description

@grompe

Trying to decompile a project with a non-Latin class name too long causes
System.IO.PathTooLongException on Linux filesystem, because in

static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName, bool treatAsPath)

, the code code is trimming to 255 UTF-16 codepoints and that can result in more than 255 UTF-8 bytes.

I'm not well-versed in the project or C#, but my quick fix was the following:

--- WholeProjectDecompiler.cs
+++ WholeProjectDecompiler.cs
@@ -727,6 +727,17 @@
 			string name = b.ToString();
 			if (extension != null)
 			{
+				if (Environment.OSVersion.Platform == PlatformID.Unix)
+				{
+					int utf8length = Encoding.UTF8.GetByteCount(name);
+					if (utf8length > maxSegmentLength - extension.Length)
+					{
+						byte[] utf8bytes = Encoding.UTF8.GetBytes(name);
+						int i = maxSegmentLength - extension.Length;
+						while (i > 0 && (utf8bytes[i] & 0xC0) == 0x80) i--;
+						name = Encoding.UTF8.GetString(utf8bytes, 0, i);
+					}
+				}
 				// make sure that adding the extension to the filename
 				// does not exceed maxSegmentLength.
 				// trim the name, if necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions