-Fixed 32 bit integer number parsing bug
This commit is contained in:
@@ -23,6 +23,8 @@
|
|||||||
// OTHER DEALINGS IN THE SOFTWARE.
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
#if NETFX_CORE
|
#if NETFX_CORE
|
||||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||||
@@ -65,8 +67,6 @@ namespace Newtonsoft.Json.Tests.Utilities
|
|||||||
Assert.AreEqual(ParseResult.Overflow, result);
|
Assert.AreEqual(ParseResult.Overflow, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
c = "-9223372036854775808".ToCharArray();
|
c = "-9223372036854775808".ToCharArray();
|
||||||
result = ConvertUtils.Int64TryParse(c, 0, c.Length, out l);
|
result = ConvertUtils.Int64TryParse(c, 0, c.Length, out l);
|
||||||
Assert.AreEqual(ParseResult.Success, result);
|
Assert.AreEqual(ParseResult.Success, result);
|
||||||
@@ -101,15 +101,6 @@ namespace Newtonsoft.Json.Tests.Utilities
|
|||||||
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
||||||
Assert.AreEqual(ParseResult.Overflow, result);
|
Assert.AreEqual(ParseResult.Overflow, result);
|
||||||
|
|
||||||
for (int j = 2; j < 10; j++)
|
|
||||||
{
|
|
||||||
c = ("2" + j + "47483647").ToCharArray();
|
|
||||||
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
|
||||||
Assert.AreEqual(ParseResult.Overflow, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
c = "-2147483648".ToCharArray();
|
c = "-2147483648".ToCharArray();
|
||||||
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
||||||
Assert.AreEqual(ParseResult.Success, result);
|
Assert.AreEqual(ParseResult.Success, result);
|
||||||
@@ -119,11 +110,30 @@ namespace Newtonsoft.Json.Tests.Utilities
|
|||||||
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
||||||
Assert.AreEqual(ParseResult.Overflow, result);
|
Assert.AreEqual(ParseResult.Overflow, result);
|
||||||
|
|
||||||
for (int j = 3; j < 10; j++)
|
for (int j = 2; j < 10; j++)
|
||||||
{
|
{
|
||||||
c = ("-2" + j + "47483648").ToCharArray();
|
for (int k = 2; k < 10; k++)
|
||||||
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
{
|
||||||
Assert.AreEqual(ParseResult.Overflow, result);
|
string t = j.ToString(CultureInfo.InvariantCulture) + k.ToString(CultureInfo.InvariantCulture) + "47483647";
|
||||||
|
|
||||||
|
c = t.ToCharArray();
|
||||||
|
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
||||||
|
|
||||||
|
Assert.AreEqual(ParseResult.Overflow, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 2; j < 10; j++)
|
||||||
|
{
|
||||||
|
for (int k = 2; k < 10; k++)
|
||||||
|
{
|
||||||
|
string t = "-" + j.ToString(CultureInfo.InvariantCulture) + k.ToString(CultureInfo.InvariantCulture) + "47483648";
|
||||||
|
|
||||||
|
c = t.ToCharArray();
|
||||||
|
result = ConvertUtils.Int32TryParse(c, 0, c.Length, out i);
|
||||||
|
|
||||||
|
Assert.AreEqual(ParseResult.Overflow, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -666,7 +666,9 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
int end = start + length;
|
int end = start + length;
|
||||||
|
|
||||||
// Int32.MaxValue and MinValue are 10 chars
|
// Int32.MaxValue and MinValue are 10 chars
|
||||||
if (length > 10)
|
// Or is 10 chars and start is greater than two
|
||||||
|
// Need to improve this!
|
||||||
|
if (length > 10 || (length == 10 && chars[start] - '0' > 2))
|
||||||
{
|
{
|
||||||
// invalid result takes precedence over overflow
|
// invalid result takes precedence over overflow
|
||||||
for (int i = start; i < end; i++)
|
for (int i = start; i < end; i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user