-Added additional validation when parsing numbers

This commit is contained in:
James Newton-King
2014-06-29 22:16:45 +12:00
parent 72874ec3a3
commit 2960a7090e
4 changed files with 28 additions and 16 deletions
@@ -210,17 +210,17 @@ namespace Newtonsoft.Json.Tests
JsonTextReader reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid number. Path '', line 1, position 2.",
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: q. Path '', line 1, position 2.",
() => { reader.Read(); });
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid decimal. Path '', line 1, position 2.",
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: q. Path '', line 1, position 2.",
() => { reader.ReadAsDecimal(); });
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid integer. Path '', line 1, position 2.",
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: q. Path '', line 1, position 2.",
() => { reader.ReadAsInt32(); });
}
@@ -1410,7 +1410,7 @@ bye", reader.Value);
ExceptionAssert.Throws<JsonReaderException>("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<JsonReaderException>("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid integer. Path '', line 1, position 77.", () => reader.ReadAsInt32());
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: s. Path '', line 1, position 77.", () => reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("1E-06"));
ExceptionAssert.Throws<JsonReaderException>("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<JsonReaderException>("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid decimal. Path '', line 1, position 77.", () => reader.ReadAsDecimal());
ExceptionAssert.Throws<JsonReaderException>("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<JsonReaderException>("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid decimal. Path '', line 1, position 77.", () => reader.Read());
ExceptionAssert.Throws<JsonReaderException>("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<JsonReaderException>("Input string '9999999999999999999999999999999999999999999999999999999999999999999999999999a' is not a valid number. Path '', line 1, position 77.", () => reader.Read());
ExceptionAssert.Throws<JsonReaderException>("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());
@@ -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")]
+19 -7
View File
@@ -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));
}
}
}
@@ -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)]