From f0c79cd0ea0e4c6d120c43402a55454165e213dd Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 15 Jun 2014 01:30:21 +1200 Subject: [PATCH] -Fixed not throwing JsonReaderException when parsing certain invalid numbers --- .../JsonTextReaderTest.cs | 21 +++++++++ .../Properties/AssemblyInfo.cs | 2 +- Src/Newtonsoft.Json/JsonTextReader.cs | 47 ++++++++++++++----- .../Properties/AssemblyInfo.cs | 2 +- .../JsonSerializerInternalReader.cs | 3 +- 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs index b049e974..94f9794b 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs @@ -47,6 +47,27 @@ namespace Newtonsoft.Json.Tests [TestFixture] public class JsonTextReaderTest : TestFixtureBase { + [Test] + public void ReadInvalidNonBase10Number() + { + string json = "0aq2dun13.hod"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + ExceptionAssert.Throws("Input string '0a' is not a valid number. Path '', line 1, position 2.", + () => { reader.Read(); }); + + reader = new JsonTextReader(new StringReader(json)); + + ExceptionAssert.Throws("Input string '0a' is not a valid decimal. Path '', line 1, position 2.", + () => { reader.ReadAsDecimal(); }); + + reader = new JsonTextReader(new StringReader(json)); + + ExceptionAssert.Throws("Input string '0a' is not a valid integer. Path '', line 1, position 2.", + () => { reader.ReadAsInt32(); }); + } + [Test] public void ThrowErrorWhenParsingUnquoteStringThatStartsWithNE() { diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index eac7b865..20a127f4 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -75,4 +75,4 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.3.17414")] +[assembly: AssemblyFileVersion("6.0.3.17415")] diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index f126cf26..9c6df61d 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -1187,12 +1187,18 @@ namespace Newtonsoft.Json { string number = _stringReference.ToString(); - // decimal.Parse doesn't support parsing hexadecimal values - int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) - ? Convert.ToInt32(number, 16) - : Convert.ToInt32(number, 8); + try + { + int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) + ? Convert.ToInt32(number, 16) + : Convert.ToInt32(number, 8); - numberValue = integer; + numberValue = integer; + } + catch (Exception ex) + { + throw JsonReaderException.Create(this, "Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, number), ex); + } } else { @@ -1219,12 +1225,19 @@ namespace Newtonsoft.Json { string number = _stringReference.ToString(); - // decimal.Parse doesn't support parsing hexadecimal values - long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) - ? Convert.ToInt64(number, 16) - : Convert.ToInt64(number, 8); + try + { + // decimal.Parse doesn't support parsing hexadecimal values + long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) + ? Convert.ToInt64(number, 16) + : Convert.ToInt64(number, 8); - numberValue = Convert.ToDecimal(integer); + numberValue = Convert.ToDecimal(integer); + } + catch (Exception ex) + { + throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number), ex); + } } else { @@ -1251,9 +1264,17 @@ namespace Newtonsoft.Json { string number = _stringReference.ToString(); - numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) - ? Convert.ToInt64(number, 16) - : Convert.ToInt64(number, 8); + try + { + numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) + ? Convert.ToInt64(number, 16) + : Convert.ToInt64(number, 8); + } + catch (Exception ex) + { + throw JsonReaderException.Create(this, "Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number), ex); + } + numberType = JsonToken.Integer; } else diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 6db7afc1..0c71142c 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -91,5 +91,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.3.17414")] +[assembly: AssemblyFileVersion("6.0.3.17415")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index db859024..abb70be0 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -2020,7 +2020,8 @@ To fix this error either change the environment to be fully trusted, change the if (property.PropertyContract == null) property.PropertyContract = GetContractSafe(property.PropertyType); - if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && property.Writable && !property.Ignored) + if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && property.Writable && !property.Ignored +) property.ValueProvider.SetValue(newObject, EnsureType(reader, property.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, property.PropertyContract, property.PropertyType)); break; case PropertyPresence.Null: