Uh oh!
There was an error while loading. Please reload this page.
Fix BigInteger.Parse returns bogus value for exponents above 1000 - #55397
Fix BigInteger.Parse returns bogus value for exponents above 1000#55397Maximys wants to merge 2 commits into
Conversation
ghost
commented
Jul 9, 2021
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsFixing invalid behaviour of BigInteger.Parse and BigInteger.ToString.
|
jeffhandley
commented
Jul 24, 2021
@pgovind This PR is assigned to you for follow-up/decision before the RC1 snap. |
pgovind
commented
Aug 2, 2021
Thanks for the contribution here @Maximys. Due to it being so late in the cycle, this is going to miss out on the .NET 6 release. We'll instead be able to review and merge it after the RC1 snap when we begin accepting more broad changes into main again (ETA: 3-4 weeks) |
Maximys
commented
Aug 3, 2021
@pgovind , ok, thank you so much, I'll wait |
jeffhandley
commented
Sep 4, 2021
@Maximys We're still trying to finish up a few things that are part of .NET 6.0 RC2. It'll be another week or two before we can review this thoroughly. I've set the milestone to 7.0.0 to indicate our intention of having this in that release though. Thanks for your contribution and patience! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Maximys
commented
Oct 2, 2021
@pgovind , I had just commit fixes by review. Can you check it? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
tannergooding
commented
Oct 4, 2021
Is there a smaller fix which only addresses the This feels like it should be two separate PRs. |
153b1ca to
e22878bCompareMaximys
commented
Oct 9, 2021
@tannergooding , I had just remove additional commits and changes from my branch. |
jeffhandley
left a comment
There was a problem hiding this comment.
Thanks for minimizing the changes, @Maximys.
@tannergooding - how does this look to you now? I honestly don't understand what the previous logic was trying to accomplish.
There was a problem hiding this comment.
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 first 9 digits giving 214748364. Multiply by 10 gives 2147483640, add 9 overflows to -2147483647. Then further multiplied by 10, gives exactly 10, adding 2 gives 12.
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.
@tannergooding , I agree with you in this case. I have idea about processing this cases
There was a problem hiding this comment.
@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.
I think we can simply make this be:
intexp=0;do{exp=exp*10+(ch-'0');ch=++p<strEnd?*p:'\0';}while((ch>='0')&&(ch<='9')&&(exp<(int.MaxValue/10)));// exp < 214748364if((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-7// but the additional complexity might not be worthwhile.Debug.Assert(exp>=214748364);returnfalse;}There was a problem hiding this comment.
The point is to account for overflow and ensure its handled so we can throw for Parse and return false for TryParse.
This trivially handles it by accounting for both cases of overflow:
- due to
exp * 10which can only overflow forexp > (int.MaxValue / 10) - due to the subsequent add which can only overflow for
exp == (int.MaxValue / 10)andch > '7'
e22878b to
494acf8Compare| { | ||
| // 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.
| // then we can technically handle one more character being 0-9 | |
| // then we can technically handle one more character being 0-7 |
2147483647 is int.MaxValue so the last digit, if we handled it, could only be 0-7
| RunCustomFormatToStringTests(s_random, "#\u2030000000", CultureInfo.CurrentCulture.NumberFormat.NegativeSign, 6, PerMilleSymbolFormatter); | ||
| } | ||
| public static IEnumerable<object[]> RunFormatScientificNotationToBigIntegerAndViceVersaData() |
There was a problem hiding this comment.
There should also be a test covering 2147483639, 2147483647, and a couple arbitrary strings bigger than that (such as 21474836492).
The latter two should end up returning false for TryParse and throwing an OverflowException to parity what int.TryParse does.
The first case will probably result in an OutOfMemoryException but otherwise should be parsed as a valid set of integer digits.
There was a problem hiding this comment.
The latter two should end up returning false for TryParse and throwing an OverflowException to parity what int.TryParse does.
Actually after thinking it over and talking with @bartonjs and @GrabYourPitchforks, I think that we should always throw PlatformNotSupportedException in this case as well. It is more consistent and allows us to expand in the future without an "additional" breaking change and avoids false being returned for a "technically valid" big integer that otherwise we simply don't support.
tannergooding
commented
Jan 7, 2022
@Maximys are you still working on this? No particular rush, just wanted to ensure that was the case since many people took a break over the holidays. There are still several pending comments above that would need to be resolved. |
ghost
commented
Jan 21, 2022
This pull request has been automatically marked |
ghost
commented
Feb 5, 2022
This pull request will now be closed since it had been marked |
dakersnar
commented
Jul 25, 2022
@tannergooding Should I try to take this over and get it across the finish line? Do we still want this for 7.0? |
tannergooding
commented
Jul 26, 2022
It's definitely something worth fixing for early .NET 8 at the very least. We can always bar check for .NET 7 |
dakersnar
commented
Jul 26, 2022
Sounds good, I'm going to cherry pick these commits and make a new PR that includes the requested tweaks. |
Fixesissue 17296