Merge pull request #2744 from akoeplinger/convert-fixes
authorMarek Safar <marek.safar@gmail.com>
Thu, 10 Mar 2016 17:43:04 +0000 (18:43 +0100)
committerMarek Safar <marek.safar@gmail.com>
Thu, 10 Mar 2016 17:43:04 +0000 (18:43 +0100)
[corlib] Fix Convert.ToUInt32 not throwing OverflowException with a too large number

mcs/class/corlib/ReferenceSources/ParseNumbers.cs
mcs/class/corlib/Test/System/ConvertTest.cs

index c87ea8f0860935c6a89c82a73368cb2dda6d5f73..9ab569e9330ac4bb2cfbfb99cfbc77e4d1d81985 100644 (file)
@@ -122,11 +122,11 @@ namespace System {
                                        throw new FormatException ("Could not find any parsable digits.");
                                }
 
-                               var res = (uint) fromBase * result + (uint) digitValue;
-                               if (res < result || res > max_value)
+                               long res = fromBase * result + digitValue;
+                               if (res > max_value)
                                        throw new OverflowException ();
                                        
-                               result = res;
+                               result = (uint)res;
                                chars++;
                                ++i;
                        }
index f9dc5929d3107412fcd6141a8da70093abad780d..2124aa85733b05943c15d6c00f8c006ed856713e 100644 (file)
@@ -4711,6 +4711,95 @@ namespace MonoTests.System {
                        Convert.ToUInt64 ("-", 16);
                }
 
+               [Test]
+               public void ToInt32_Base10_MaxValue ()
+               {
+                       Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 (Int32.MaxValue.ToString(), 10));
+               }
+
+               [Test]
+               [ExpectedException (typeof (OverflowException))]
+               [Category ("NotWorking")] // FIXME: this should throw an OverflowException but currently doesn't
+               public void ToInt32_Base10_MaxValueOverflow ()
+               {
+                       var overflowValue = ((UInt32) Int32.MaxValue) + 1;
+                       Convert.ToInt32 (overflowValue.ToString (), 10);
+               }
+
+               [Test]
+               public void ToInt32_Base10_MinValue ()
+               {
+                       Assert.AreEqual (Int32.MinValue, Convert.ToInt32 (Int32.MinValue.ToString(), 10));
+               }
+
+               [Test]
+               [ExpectedException (typeof (OverflowException))]
+               [Category ("NotWorking")] // FIXME: this should throw an OverflowException but currently doesn't                
+               public void ToInt32_Base10_MinValueOverflow ()
+               {
+                       var overflowValue = ((UInt32) Int32.MaxValue) + 2;
+                       Convert.ToInt32 ("-" + overflowValue.ToString (), 10);
+               }
+
+               [Test]
+               public void ToInt32_Base16_MaxValue ()
+               {
+                       Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 (Int32.MaxValue.ToString("x"), 16));
+               }
+
+               [Test]
+               public void ToInt32_Base16_MaxValueOverflow ()
+               {
+                       var overflowValue = ((UInt32) Int32.MaxValue) + 1;
+                       Assert.AreEqual (-2147483648, Convert.ToInt32 (overflowValue.ToString("x"), 16));
+               }
+
+               [Test]
+               [ExpectedException (typeof (OverflowException))]
+               public void ToInt32_Base16_MaxValueOverflow2 ()
+               {
+                       Convert.ToInt32 (UInt32.MaxValue.ToString ("x") + "0", 16);
+               }
+
+               [Test]
+               public void ToInt32_Base16_MinValue ()
+               {
+                       Assert.AreEqual (Int32.MinValue, Convert.ToInt32 (Int32.MinValue.ToString ("x"), 16));
+               }
+
+               [Test]
+               public void ToUInt32_Base10_MaxValue ()
+               {
+                       Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 (UInt32.MaxValue.ToString (), 10));
+               }
+
+               [Test]
+               [ExpectedException (typeof (OverflowException))]
+               public void ToUInt32_Base10_MaxValueOverflow ()
+               {
+                       Convert.ToUInt32 (UInt32.MaxValue.ToString () + "0", 10);
+               }
+
+               [Test]
+               [ExpectedException (typeof (OverflowException))]
+               public void ToUInt32_Base10_MaxValueOverflow2 ()
+               {
+                       Convert.ToUInt32 ("4933891728", 10);
+               }
+
+               [Test]
+               public void ToUInt32_Base16_MaxValue ()
+               {
+                       Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 (UInt32.MaxValue.ToString ("x"), 16));
+               }
+
+               [Test]
+               [ExpectedException (typeof (OverflowException))]
+               public void ToUInt32_Base16_MaxValueOverflow ()
+               {
+                       Convert.ToUInt32 (UInt32.MaxValue.ToString ("x") + "0", 16);
+               }
+
                [Test]
                public void ToInt64_Base10_MaxValue ()
                {
@@ -4788,7 +4877,7 @@ namespace MonoTests.System {
                [ExpectedException (typeof (OverflowException))]
                public void ToUInt64_Base16_MaxValueOverflow ()
                {
-                       Convert.ToInt64 (UInt64.MaxValue.ToString ("x") + "0", 16);
+                       Convert.ToUInt64 (UInt64.MaxValue.ToString ("x") + "0", 16);
                }
 
                [Test] // bug #481687