-Fixed not overflowing during integer parse
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.Numerics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
#if !NETFX_CORE
|
#if !NETFX_CORE
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
@@ -770,6 +771,93 @@ Parameter name: reader",
|
|||||||
Assert.AreEqual(JsonToken.Null, reader.TokenType);
|
Assert.AreEqual(JsonToken.Null, reader.TokenType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ReadInt32Overflow()
|
||||||
|
{
|
||||||
|
long i = int.MaxValue;
|
||||||
|
|
||||||
|
JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.Read();
|
||||||
|
Assert.AreEqual(typeof(long), reader.ValueType);
|
||||||
|
|
||||||
|
for (int j = 1; j < 1000; j++)
|
||||||
|
{
|
||||||
|
long total = j + i;
|
||||||
|
ExceptionAssert.Throws<OverflowException>(
|
||||||
|
"Arithmetic operation resulted in an overflow.",
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.ReadAsInt32();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ReadInt32Overflow_Negative()
|
||||||
|
{
|
||||||
|
long i = int.MinValue;
|
||||||
|
|
||||||
|
JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.Read();
|
||||||
|
Assert.AreEqual(typeof(long), reader.ValueType);
|
||||||
|
Assert.AreEqual(i, reader.Value);
|
||||||
|
|
||||||
|
for (int j = 1; j < 1000; j++)
|
||||||
|
{
|
||||||
|
long total = -j + i;
|
||||||
|
ExceptionAssert.Throws<OverflowException>(
|
||||||
|
"Arithmetic operation resulted in an overflow.",
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.ReadAsInt32();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
|
||||||
|
[Test]
|
||||||
|
public void ReadInt64Overflow()
|
||||||
|
{
|
||||||
|
BigInteger i = new BigInteger(long.MaxValue);
|
||||||
|
|
||||||
|
JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.Read();
|
||||||
|
Assert.AreEqual(typeof(long), reader.ValueType);
|
||||||
|
|
||||||
|
for (int j = 1; j < 1000; j++)
|
||||||
|
{
|
||||||
|
BigInteger total = i + j;
|
||||||
|
|
||||||
|
reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.Read();
|
||||||
|
|
||||||
|
Assert.AreEqual(typeof(BigInteger), reader.ValueType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ReadInt64Overflow_Negative()
|
||||||
|
{
|
||||||
|
BigInteger i = new BigInteger(long.MinValue);
|
||||||
|
|
||||||
|
JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.Read();
|
||||||
|
Assert.AreEqual(typeof(long), reader.ValueType);
|
||||||
|
|
||||||
|
for (int j = 1; j < 1000; j++)
|
||||||
|
{
|
||||||
|
BigInteger total = i + -j;
|
||||||
|
|
||||||
|
reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
reader.Read();
|
||||||
|
|
||||||
|
Assert.AreEqual(typeof(BigInteger), reader.ValueType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void AppendCharsWhileReadingNewLine()
|
public void AppendCharsWhileReadingNewLine()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -75,4 +75,4 @@ using System.Security;
|
|||||||
// You can specify all the values or you can default the Revision and Build Numbers
|
// You can specify all the values or you can default the Revision and Build Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
[assembly: AssemblyFileVersion("5.0.4.16022")]
|
[assembly: AssemblyFileVersion("5.0.4.16023")]
|
||||||
|
|||||||
@@ -92,6 +92,6 @@ using System.Security;
|
|||||||
// You can specify all the values or you can default the Revision and Build Numbers
|
// You can specify all the values or you can default the Revision and Build Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
[assembly: AssemblyFileVersion("5.0.4.16022")]
|
[assembly: AssemblyFileVersion("5.0.4.16023")]
|
||||||
|
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
@@ -619,7 +619,13 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
// go from negative to positive to avoids overflow
|
// go from negative to positive to avoids overflow
|
||||||
// negative can be slightly bigger than positive
|
// negative can be slightly bigger than positive
|
||||||
if (!isNegative)
|
if (!isNegative)
|
||||||
|
{
|
||||||
|
// negative integer can be one bigger than positive
|
||||||
|
if (result == int.MinValue)
|
||||||
|
throw new OverflowException();
|
||||||
|
|
||||||
result = -result;
|
result = -result;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -664,7 +670,13 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
// go from negative to positive to avoids overflow
|
// go from negative to positive to avoids overflow
|
||||||
// negative can be slightly bigger than positive
|
// negative can be slightly bigger than positive
|
||||||
if (!isNegative)
|
if (!isNegative)
|
||||||
|
{
|
||||||
|
// negative integer can be one bigger than positive
|
||||||
|
if (value == long.MinValue)
|
||||||
|
return ParseResult.Overflow;
|
||||||
|
|
||||||
value = -value;
|
value = -value;
|
||||||
|
}
|
||||||
|
|
||||||
return ParseResult.Success;
|
return ParseResult.Success;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user