From 2960a7090ec0cfd7096892db10210c896303f3d0 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 29 Jun 2014 22:16:45 +1200 Subject: [PATCH] -Added additional validation when parsing numbers --- .../JsonTextReaderTest.cs | 14 +++++----- .../Properties/AssemblyInfo.cs | 2 +- Src/Newtonsoft.Json/JsonTextReader.cs | 26 ++++++++++++++----- .../Properties/AssemblyInfo.cs | 2 +- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs index 28ce16fd..8089b632 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs @@ -210,17 +210,17 @@ namespace Newtonsoft.Json.Tests JsonTextReader reader = new JsonTextReader(new StringReader(json)); - ExceptionAssert.Throws("Input string '0a' is not a valid number. Path '', line 1, position 2.", + ExceptionAssert.Throws("Unexpected character encountered while parsing number: q. 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.", + ExceptionAssert.Throws("Unexpected character encountered while parsing number: q. 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.", + ExceptionAssert.Throws("Unexpected character encountered while parsing number: q. Path '', line 1, position 2.", () => { reader.ReadAsInt32(); }); } @@ -1410,7 +1410,7 @@ bye", reader.Value); ExceptionAssert.Throws("JSON integer 9223372036854775807 is too large or small for an Int32. Path '', line 1, position 19.", () => reader.ReadAsInt32()); reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); - ExceptionAssert.Throws("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid integer. Path '', line 1, position 77.", () => reader.ReadAsInt32()); + ExceptionAssert.Throws("Unexpected character encountered while parsing number: s. Path '', line 1, position 77.", () => reader.ReadAsInt32()); reader = new JsonTextReader(new StringReader("1E-06")); ExceptionAssert.Throws("Input string '1E-06' is not a valid integer. Path '', line 1, position 5.", () => reader.ReadAsInt32()); @@ -1443,11 +1443,11 @@ bye", reader.Value); Assert.AreEqual(0, reader.ReadAsDecimal()); reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); - ExceptionAssert.Throws("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid decimal. Path '', line 1, position 77.", () => reader.ReadAsDecimal()); + ExceptionAssert.Throws("Unexpected character encountered while parsing number: s. Path '', line 1, position 77.", () => reader.ReadAsDecimal()); reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); reader.FloatParseHandling = Json.FloatParseHandling.Decimal; - ExceptionAssert.Throws("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid decimal. Path '', line 1, position 77.", () => reader.Read()); + ExceptionAssert.Throws("Unexpected character encountered while parsing number: s. Path '', line 1, position 77.", () => reader.Read()); reader = new JsonTextReader(new StringReader("1E-06")); Assert.AreEqual(0.000001m, reader.ReadAsDecimal()); @@ -1485,7 +1485,7 @@ bye", reader.Value); Assert.AreEqual(-0.0d, reader.Value); reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); - ExceptionAssert.Throws("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid number. Path '', line 1, position 77.", () => reader.Read()); + ExceptionAssert.Throws("Unexpected character encountered while parsing number: s. Path '', line 1, position 77.", () => reader.Read()); reader = new JsonTextReader(new StringReader("1E-06")); Assert.IsTrue(reader.Read()); diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 1c1562c1..9dafc78f 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.17428")] +[assembly: AssemblyFileVersion("6.0.3.17429")] diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index b91d74d7..989a7e02 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -631,19 +631,18 @@ namespace Newtonsoft.Json while (true) { - switch (_chars[charPos++]) + switch (_chars[charPos]) { case '\0': - if (_charsUsed == charPos - 1) + _charPos = charPos; + + if (_charsUsed == charPos) { - charPos--; - _charPos = charPos; if (ReadData(true) == 0) return; } else { - _charPos = charPos - 1; return; } break; @@ -674,10 +673,23 @@ namespace Newtonsoft.Json case '7': case '8': case '9': + charPos++; break; default: - _charPos = charPos - 1; - return; + _charPos = charPos; + + char currentChar = _chars[_charPos]; + if (char.IsWhiteSpace(currentChar) + || currentChar == ',' + || currentChar == '}' + || currentChar == ']' + || currentChar == ')' + || currentChar == '/') + { + return; + } + + throw JsonReaderException.Create(this, "Unexpected character encountered while parsing number: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); } } } diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index aa1f6de7..bf1a0990 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.17428")] +[assembly: AssemblyFileVersion("6.0.3.17429")] [assembly: CLSCompliant(true)]