ILSpy
ILSpy copied to clipboard
System.IO.PathTooLongException on Linux because limit is 255 UTF-8 bytes
Trying to decompile a project with a non-Latin class name too long causes System.IO.PathTooLongException on Linux filesystem, because in https://github.com/icsharpcode/ILSpy/blob/167192eb75855e6a1a5321c846094af025eb0d0f/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs#L652 , 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.