diff options
author | Rodrigo Kumpera <kumpera@gmail.com> | 2014-07-09 04:02:19 +0400 |
---|---|---|
committer | Rodrigo Kumpera <kumpera@gmail.com> | 2014-07-09 04:02:19 +0400 |
commit | aa217360cebbd6f01e02b54aa0d4f3c2f917fddf (patch) | |
tree | 41f6d8aafd4e038146006dc2f07b43a68e152301 /mcs/class/System.Numerics | |
parent | 2fa4d250534d794b9aecab5abcdb6daf33e3c11e (diff) |
[System.Numerics] Contract the values array when decoding an array with a negative number and a trailer that evals to zero. Fixes #20456.
Diffstat (limited to 'mcs/class/System.Numerics')
-rw-r--r-- | mcs/class/System.Numerics/System.Numerics/BigInteger.cs | 6 | ||||
-rw-r--r-- | mcs/class/System.Numerics/Test/System.Numerics/BigIntegerTest.cs | 17 |
2 files changed, 15 insertions, 8 deletions
diff --git a/mcs/class/System.Numerics/System.Numerics/BigInteger.cs b/mcs/class/System.Numerics/System.Numerics/BigInteger.cs index b067997cb7c..329f91eed1f 100644 --- a/mcs/class/System.Numerics/System.Numerics/BigInteger.cs +++ b/mcs/class/System.Numerics/System.Numerics/BigInteger.cs @@ -315,12 +315,14 @@ namespace System.Numerics { word = (uint)sub; borrow = (uint)(sub >> 32) & 0x1u; - data [data.Length - 1] = ~word & store_mask; + if ((~word & store_mask) == 0) + data = Resize (data, data.Length - 1); + else + data [data.Length - 1] = ~word & store_mask; } if (borrow != 0) //FIXME I believe this can't happen, can someone write a test for it? throw new Exception ("non zero final carry"); } - } public bool IsEven { diff --git a/mcs/class/System.Numerics/Test/System.Numerics/BigIntegerTest.cs b/mcs/class/System.Numerics/Test/System.Numerics/BigIntegerTest.cs index 4aa0ffbd029..817d13e40c1 100644 --- a/mcs/class/System.Numerics/Test/System.Numerics/BigIntegerTest.cs +++ b/mcs/class/System.Numerics/Test/System.Numerics/BigIntegerTest.cs @@ -550,13 +550,18 @@ namespace MonoTests.System.Numerics { long[] values = new long [] { 0, long.MinValue, long.MaxValue, -1, 1L + int.MaxValue, -1L + int.MinValue, 0x1234, 0xFFFFFFFFL, 0x1FFFFFFFFL, -0xFFFFFFFFL, -0x1FFFFFFFFL, - 0x100000000L, -0x100000000L, 0x100000001L, -0x100000001L }; + 0x100000000L, -0x100000000L, 0x100000001L, -0x100000001L, 4294967295L, -4294967295L, 4294967296L, -4294967296L }; foreach (var val in values) { - var a = new BigInteger (val); - var b = new BigInteger (a.ToByteArray ()); - - Assert.AreEqual (val, (long)a, "#a_" + val); - Assert.AreEqual (val, (long)b, "#b_" + val); + try { + var a = new BigInteger (val); + var b = new BigInteger (a.ToByteArray ()); + + Assert.AreEqual (val, (long)a, "#a_" + val); + Assert.AreEqual (val, (long)b, "#b_" + val); + Assert.AreEqual (a, b, "#a == #b (" + val + ")"); + } catch (Exception e) { + Assert.Fail ("could not roundtrip {0}", val); + } } } |