From 115ca210c788d82d0d98872d57f59083acd6529b Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 14 Jan 2021 20:45:35 +0100 Subject: [PATCH] Fix #2275: Exception when assembly does not contain proper MetadataVersion. --- .../Metadata/DotNetCorePathFinderExtensions.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ICSharpCode.Decompiler/Metadata/DotNetCorePathFinderExtensions.cs b/ICSharpCode.Decompiler/Metadata/DotNetCorePathFinderExtensions.cs index aea9fea59..a10373c56 100644 --- a/ICSharpCode.Decompiler/Metadata/DotNetCorePathFinderExtensions.cs +++ b/ICSharpCode.Decompiler/Metadata/DotNetCorePathFinderExtensions.cs @@ -132,16 +132,20 @@ namespace ICSharpCode.Decompiler.Metadata */ var pathMatch = Regex.Match(assemblyPath, PathPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.ExplicitCapture); + string version; if (pathMatch.Success) { var type = pathMatch.Groups["type"].Value; - var version = pathMatch.Groups["version"].Value; + version = pathMatch.Groups["version"].Value; if (string.IsNullOrEmpty(version)) version = reader.MetadataVersion; + if (string.IsNullOrEmpty(version)) + version = "4.0"; + version = version.TrimStart('v'); if (type == "Microsoft.NET" || type == ".NETFramework") { - return $".NETFramework,Version=v{version.TrimStart('v').Substring(0, 3)}"; + return $".NETFramework,Version=v{version.Substring(0, Math.Min(3, version.Length))}"; } else if (type.IndexOf("netcore", StringComparison.OrdinalIgnoreCase) >= 0) { @@ -154,7 +158,11 @@ namespace ICSharpCode.Decompiler.Metadata } else { - return $".NETFramework,Version={reader.MetadataVersion.Substring(0, 4)}"; + version = reader.MetadataVersion; + if (string.IsNullOrEmpty(version)) + version = "4.0"; + version = version.TrimStart('v'); + return $".NETFramework,Version=v{version.Substring(0, Math.Min(3, version.Length))}"; } }