-Added additional validation when parsing numbers
This commit is contained in:
@@ -210,17 +210,17 @@ namespace Newtonsoft.Json.Tests
|
|||||||
|
|
||||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
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.Read(); });
|
||||||
|
|
||||||
reader = new JsonTextReader(new StringReader(json));
|
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.ReadAsDecimal(); });
|
||||||
|
|
||||||
reader = new JsonTextReader(new StringReader(json));
|
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(); });
|
() => { 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());
|
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"));
|
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"));
|
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());
|
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());
|
Assert.AreEqual(0, reader.ReadAsDecimal());
|
||||||
|
|
||||||
reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
|
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 = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
|
||||||
reader.FloatParseHandling = Json.FloatParseHandling.Decimal;
|
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"));
|
reader = new JsonTextReader(new StringReader("1E-06"));
|
||||||
Assert.AreEqual(0.000001m, reader.ReadAsDecimal());
|
Assert.AreEqual(0.000001m, reader.ReadAsDecimal());
|
||||||
@@ -1485,7 +1485,7 @@ bye", reader.Value);
|
|||||||
Assert.AreEqual(-0.0d, reader.Value);
|
Assert.AreEqual(-0.0d, reader.Value);
|
||||||
|
|
||||||
reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
|
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"));
|
reader = new JsonTextReader(new StringReader("1E-06"));
|
||||||
Assert.IsTrue(reader.Read());
|
Assert.IsTrue(reader.Read());
|
||||||
|
|||||||
@@ -75,4 +75,4 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("6.0.0.0")]
|
[assembly: AssemblyVersion("6.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("6.0.3.17428")]
|
[assembly: AssemblyFileVersion("6.0.3.17429")]
|
||||||
|
|||||||
@@ -631,19 +631,18 @@ namespace Newtonsoft.Json
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
switch (_chars[charPos++])
|
switch (_chars[charPos])
|
||||||
{
|
{
|
||||||
case '\0':
|
case '\0':
|
||||||
if (_charsUsed == charPos - 1)
|
_charPos = charPos;
|
||||||
|
|
||||||
|
if (_charsUsed == charPos)
|
||||||
{
|
{
|
||||||
charPos--;
|
|
||||||
_charPos = charPos;
|
|
||||||
if (ReadData(true) == 0)
|
if (ReadData(true) == 0)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_charPos = charPos - 1;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -674,10 +673,23 @@ namespace Newtonsoft.Json
|
|||||||
case '7':
|
case '7':
|
||||||
case '8':
|
case '8':
|
||||||
case '9':
|
case '9':
|
||||||
|
charPos++;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
_charPos = charPos - 1;
|
_charPos = charPos;
|
||||||
return;
|
|
||||||
|
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:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("6.0.0.0")]
|
[assembly: AssemblyVersion("6.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("6.0.3.17428")]
|
[assembly: AssemblyFileVersion("6.0.3.17429")]
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
Reference in New Issue
Block a user