diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs index 64bf7f05..5513b257 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs @@ -26,6 +26,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Numerics; using System.Text; #if !NETFX_CORE using NUnit.Framework; @@ -770,6 +771,93 @@ Parameter name: reader", 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( + "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( + "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] public void AppendCharsWhileReadingNewLine() { diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 40034acc..f776820d 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -75,4 +75,4 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.4.16022")] +[assembly: AssemblyFileVersion("5.0.4.16023")] diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 72bc6abf..668b655c 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -92,6 +92,6 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.4.16022")] +[assembly: AssemblyFileVersion("5.0.4.16023")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index d0c54ce2..10aef2fa 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -619,7 +619,13 @@ namespace Newtonsoft.Json.Utilities // go from negative to positive to avoids overflow // negative can be slightly bigger than positive if (!isNegative) + { + // negative integer can be one bigger than positive + if (result == int.MinValue) + throw new OverflowException(); + result = -result; + } return result; } @@ -664,7 +670,13 @@ namespace Newtonsoft.Json.Utilities // go from negative to positive to avoids overflow // negative can be slightly bigger than positive if (!isNegative) + { + // negative integer can be one bigger than positive + if (value == long.MinValue) + return ParseResult.Overflow; + value = -value; + } return ParseResult.Success; }