diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs index d3b6539f..ca628dcc 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs @@ -1260,6 +1260,9 @@ bye", reader.Value); reader = new JsonTextReader(new StringReader(long.MaxValue.ToString())); ExceptionAssert.Throws("Arithmetic operation resulted in an overflow.", () => reader.ReadAsInt32()); + reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); + ExceptionAssert.Throws("Input string was not in a correct format.", () => reader.ReadAsInt32()); + reader = new JsonTextReader(new StringReader("1E-06")); ExceptionAssert.Throws("Input string was not in a correct format.", () => reader.ReadAsInt32()); diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index b1c124c3..2fe19961 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -446,6 +446,16 @@ namespace Newtonsoft.Json.Tests.Serialization } #endif + [Test] + public void DeserializeLargeFloat() + { + object o = JsonConvert.DeserializeObject("100000000000000000000000000000000000000.0"); + + CustomAssert.IsInstanceOfType(typeof(double), o); + + Assert.IsTrue(MathUtils.ApproxEquals(1E+38, (double)o)); + } + [Test] public void SerializeDeserializeRegex() { diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 88807518..b1a55097 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -1194,8 +1194,10 @@ To fix this error either change the JSON to a {1} or change the deserialized typ int initialDepth = reader.Depth; - JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType); - JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract, containerProperty); + if (contract.ItemContract == null) + contract.ItemContract = GetContractSafe(contract.CollectionItemType); + + JsonConverter collectionItemConverter = GetConverter(contract.ItemContract, null, contract, containerProperty); int? previousErrorIndex = null; @@ -1204,7 +1206,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ { try { - if (ReadForType(reader, collectionItemContract, collectionItemConverter != null)) + if (ReadForType(reader, contract.ItemContract, collectionItemConverter != null)) { switch (reader.TokenType) { @@ -1219,7 +1221,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ if (collectionItemConverter != null && collectionItemConverter.CanRead) value = DeserializeConvertable(collectionItemConverter, reader, contract.CollectionItemType, null); else - value = CreateValueInternal(reader, contract.CollectionItemType, collectionItemContract, null, contract, containerProperty, null); + value = CreateValueInternal(reader, contract.CollectionItemType, contract.ItemContract, null, contract, containerProperty, null); list.Add(value); break; diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index 95bd854b..510f8505 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -611,7 +611,21 @@ namespace Newtonsoft.Json.Utilities // overflow has caused the number to loop around if (newValue > result) + { + i++; + + // double check the rest of the string that there wasn't anything invalid + // invalid result takes precedence over overflow result + for (; i < end; i++) + { + c = chars[i] - '0'; + + if (c < 0 || c > 9) + throw new FormatException("Input string was not in a correct format."); + } + throw new OverflowException(); + } result = newValue; } @@ -662,7 +676,22 @@ namespace Newtonsoft.Json.Utilities // overflow has caused the number to loop around if (newValue > value) + { + i++; + + // double check the rest of the string that there wasn't anything invalid + // invalid result takes precedence over overflow result + for (; i < end; i++) + { + c = chars[i] - '0'; + + if (c < 0 || c > 9) + return ParseResult.Invalid; + } + return ParseResult.Overflow; + + } value = newValue; }