Fix potential null ref in RuntimeInformation.FrameworkDescription (dotnet/corefx...
authorStephen Toub <stoub@microsoft.com>
Thu, 15 Aug 2019 02:05:18 +0000 (22:05 -0400)
committerGitHub <noreply@github.com>
Thu, 15 Aug 2019 02:05:18 +0000 (22:05 -0400)
If there's no FX_PRODUCT_VERSION and then if there's no AssemblyInformationalVersionAttribute on the assembly or its InformationVersion is null, we'll try to dereference a null string.

Commit migrated from https://github.com/dotnet/corefx/commit/c08abbe3527fca16b55df53a596164fac97031d4

src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs

index 6a91c1e..2f4cf92 100644 (file)
@@ -3,7 +3,6 @@
 // See the LICENSE file in the project root for more information.
 
 using System.Reflection;
-using System.Diagnostics;
 
 namespace System.Runtime.InteropServices
 {
@@ -26,12 +25,17 @@ namespace System.Runtime.InteropServices
                         versionString = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
 
                         // Strip the git hash if there is one
-                        int plusIndex = versionString.IndexOf('+');
-                        if (plusIndex != -1)
-                            versionString = versionString.Substring(0, plusIndex);
+                        if (versionString != null)
+                        {
+                            int plusIndex = versionString.IndexOf('+');
+                            if (plusIndex != -1)
+                            {
+                                versionString = versionString.Substring(0, plusIndex);
+                            }
+                        }
                     }
 
-                    s_frameworkDescription = $"{FrameworkName} {versionString}";
+                    s_frameworkDescription = !string.IsNullOrWhiteSpace(versionString) ? $"{FrameworkName} {versionString}" : FrameworkName;
                 }
 
                 return s_frameworkDescription;