Fixed overflowing exponent bug

This commit is contained in:
TylerBrinkley
2016-09-01 14:05:49 -05:00
parent 5fd78b21f5
commit 62470bd803
2 changed files with 27 additions and 27 deletions
@@ -125,6 +125,10 @@ namespace Newtonsoft.Json.Tests.Utilities
AssertDoubleTryParse("1.7976931348623159E+308", ParseResult.Overflow, null); AssertDoubleTryParse("1.7976931348623159E+308", ParseResult.Overflow, null);
AssertDoubleTryParse("-1.7976931348623159E+308", ParseResult.Overflow, null); AssertDoubleTryParse("-1.7976931348623159E+308", ParseResult.Overflow, null);
AssertDoubleTryParse("1E4294967297", ParseResult.Overflow, null);
AssertDoubleTryParse("1E4294967297B", ParseResult.Invalid, null);
AssertDoubleTryParse("1E-4294967297", ParseResult.Success, 0);
} }
[Test] [Test]
+22 -26
View File
@@ -997,7 +997,6 @@ namespace Newtonsoft.Json.Utilities
int numDecimalStart = end; int numDecimalStart = end;
int numDecimalEnd = end; int numDecimalEnd = end;
int exponent = 0; int exponent = 0;
bool exponentNegative = false;
ulong mantissa = 0UL; ulong mantissa = 0UL;
int mantissaDigits = 0; int mantissaDigits = 0;
int exponentFromMantissa = 0; int exponentFromMantissa = 0;
@@ -1043,6 +1042,7 @@ namespace Newtonsoft.Json.Utilities
} }
c = chars[i]; c = chars[i];
bool exponentNegative = false;
switch (c) switch (c)
{ {
case '-': case '-':
@@ -1055,7 +1055,7 @@ namespace Newtonsoft.Json.Utilities
} }
// parse 3 digit // parse 3 digit
for (;i < end;i++) for (; i < end; i++)
{ {
c = chars[i]; c = chars[i];
if (c < '0' || c > '9') if (c < '0' || c > '9')
@@ -1063,20 +1063,18 @@ namespace Newtonsoft.Json.Utilities
return ParseResult.Invalid; return ParseResult.Invalid;
} }
exponent = (10 * exponent) + (c - '0'); var newExponent = (10 * exponent) + (c - '0');
// stops updating exponent when overflowing
if (exponent < newExponent)
{
exponent = newExponent;
}
} }
if (exponentNegative) if (exponentNegative)
{ {
exponent = -exponent; exponent = -exponent;
} }
else
{
if (exponent > MaxExponent)
{
return ParseResult.Overflow;
}
}
break; break;
default: default:
if (c < '0' || c > '9') if (c < '0' || c > '9')
@@ -1102,9 +1100,16 @@ namespace Newtonsoft.Json.Utilities
// correct the decimal point // correct the decimal point
exponent -= (numDecimalEnd - numDecimalStart); exponent -= (numDecimalEnd - numDecimalStart);
if (exponent > MaxExponent)
{
return ParseResult.Overflow;
}
value = mantissa;
if (exponent > 0) if (exponent > 0)
{ {
value = mantissa * DoubleExponents[exponent - 1]; value *= DoubleExponents[exponent - 1];
} }
else if (exponent < 0) else if (exponent < 0)
{ {
@@ -1112,26 +1117,17 @@ namespace Newtonsoft.Json.Utilities
if (exponent > MaxExponent) if (exponent > MaxExponent)
{ {
// handle very small numbers, e.g. 4.94065645841247E-324 // handle very small numbers, e.g. 4.94065645841247E-324
value = mantissa * DoubleNegativeExponents[154 - 1];
exponent -= 154; exponent -= 154;
} if (DoubleNegativeExponents.Length <= exponent - 1)
else {
{ value = 0D;
value = mantissa; return ParseResult.Success;
} }
value *= DoubleNegativeExponents[154 - 1];
if (DoubleNegativeExponents.Length <= exponent - 1)
{
value = 0;
return ParseResult.Success;
} }
value = exponent < 23 ? value / DoubleExponents[exponent - 1] : value * DoubleNegativeExponents[exponent - 1]; value = exponent < 23 ? value / DoubleExponents[exponent - 1] : value * DoubleNegativeExponents[exponent - 1];
} }
else
{
value = mantissa;
}
if (double.IsPositiveInfinity(value)) if (double.IsPositiveInfinity(value))
{ {