Fix BigInteger hex string parsing bug (#54251) (#54262)
authorJoseph Da Silva <39675835+jfd16@users.noreply.github.com>
Sat, 26 Jun 2021 19:33:01 +0000 (12:33 -0700)
committerGitHub <noreply@github.com>
Sat, 26 Jun 2021 19:33:01 +0000 (15:33 -0400)
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigNumber.cs
src/libraries/System.Runtime.Numerics/tests/BigInteger/parse.cs

index 957bd73..5ce7919 100644 (file)
@@ -467,8 +467,8 @@ namespace System.Numerics
 
                     if ((!isNegative && sign < 0) || sign == int.MinValue)
                     {
-                        sign = isNegative ? -1 : 1;
                         bits = new[] { (uint)sign };
+                        sign = isNegative ? -1 : 1;
                     }
                 }
                 else
index 23440b4..115a613 100644 (file)
@@ -105,6 +105,19 @@ namespace System.Numerics.Tests
             Assert.Equal(0, result);
         }
 
+        [Fact]
+        public void Parse_Hex32Bits()
+        {
+            // Regression test for: https://github.com/dotnet/runtime/issues/54251
+            BigInteger result;
+
+            Assert.True(BigInteger.TryParse("80000000", NumberStyles.HexNumber, null, out result));
+            Assert.Equal(int.MinValue, result);
+
+            Assert.True(BigInteger.TryParse("080000001", NumberStyles.HexNumber, null, out result));
+            Assert.Equal(0x80000001u, result);
+        }
+
         private static void RunFormatProviderParseStrings()
         {
             NumberFormatInfo nfi = new NumberFormatInfo();