-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix BigInteger.Parse returns bogus value for exponents above 1000 #55397
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -517,15 +517,19 @@ private static unsafe bool ParseNumber(ref char* str, char* strEnd, NumberStyles | |||||
{ | ||||||
exp = exp * 10 + (ch - '0'); | ||||||
ch = ++p < strEnd ? *p : '\0'; | ||||||
if (exp > 1000) | ||||||
{ | ||||||
exp = 9999; | ||||||
while (ch >= '0' && ch <= '9') | ||||||
{ | ||||||
ch = ++p < strEnd ? *p : '\0'; | ||||||
} | ||||||
} | ||||||
} while (ch >= '0' && ch <= '9'); | ||||||
} while ((ch >= '0') && (ch <= '9') && (exp < int.MaxValue / 10)); | ||||||
|
||||||
if ((ch >= '0') && (ch <= '9')) | ||||||
{ | ||||||
// We still had remaining characters but bailed early because | ||||||
// the exponent was going to overflow. If exp is exactly 214748364 | ||||||
// then we can technically handle one more character being 0-9 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// but the additional complexity might not be worthwhile. | ||||||
|
||||||
Debug.Assert(exp >= 214748364); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (negExp) | ||||||
{ | ||||||
exp = -exp; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -439,6 +439,25 @@ public static void CustomFormatPerMille() | |
RunCustomFormatToStringTests(s_random, "#\u2030000000", CultureInfo.CurrentCulture.NumberFormat.NegativeSign, 6, PerMilleSymbolFormatter); | ||
} | ||
|
||
public static IEnumerable<object[]> RunFormatScientificNotationToBigIntegerAndViceVersaData() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should also be a test covering The latter two should end up returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually after thinking it over and talking with @bartonjs and @GrabYourPitchforks, I think that we should always throw |
||
{ | ||
yield return new object[] { "1E+1000", "1E+1000" }; | ||
yield return new object[] { "1E+1001", "1E+1001" }; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(RunFormatScientificNotationToBigIntegerAndViceVersaData))] | ||
public static void RunFormatScientificNotationToBigIntegerAndViceVersa(string testingValue, string expectedResult) | ||
{ | ||
BigInteger parsedValue; | ||
string actualResult; | ||
|
||
parsedValue = BigInteger.Parse(testingValue, NumberStyles.AllowExponent); | ||
actualResult = parsedValue.ToString("E0"); | ||
|
||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
|
||
private static void RunSimpleProviderToStringTests(Random random, string format, NumberFormatInfo provider, int precision, StringFormatter formatter) | ||
{ | ||
string test; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay in review here.
This isn't quite the right fix and likely introduces a different bug. For example, if you have an exponent of
21474836492
it will succesfully parse the first9
digits giving214748364
. Multiply by 10 gives2147483640
, add9
overflows to-2147483647
. Then further multiplied by 10, gives exactly10
, adding2
gives12
.Both of these last two digits are problematic because of this and so we need something that accounts for overflow here and surfaces it as invalid input/failure to parse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tannergooding , I agree with you in this case. I have idea about processing this cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tannergooding , I had just repeat my summer investigation about algorithms of BigInteger, and I remember, that there is BigNumber class with BigNumberBuffer inside it and this buffer contains scale with int type.
BigNumber provide some functionality to BigInteger and if int type with it's MaxValue is invalid, then .NET Command should create separate task for change it.
But firstly, lets make little calculation:
itemSize * arraySize = 32bit * 2147483647 = 68719476704 bit = 8589934588 byte ≈ 8388608 kbyte = 8192 mb = 8 gb
thus, theoretically we can provide 8 Gb for store one BigInteger by current architecture! I think, this is more than enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simply make this be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is to account for overflow and ensure its handled so we can throw for
Parse
and returnfalse
forTryParse
.This trivially handles it by accounting for both cases of overflow:
exp * 10
which can only overflow forexp > (int.MaxValue / 10)
exp == (int.MaxValue / 10)
andch > '7'