-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
IBinaryInteger<BigInteger>.GetShortestBitLength() - int overflow is not handled #84670
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsDescriptionWhen bit length of BigInteger value is greater than Reproduction StepsCode: using System.Numerics;
var l1 = 42;
var l2 = int.MaxValue;
BigInteger b = (BigInteger.One<<l1)<<l2;
var ll = 1L + l1 + l2;
Console.WriteLine(ll);
var res = ((IBinaryInteger<BigInteger>)b).GetShortestBitLength();
Console.WriteLine(res); Expected behaviorException on Actual behavioroutput
Regression?No Known WorkaroundsNo ConfigurationThe latest Other informationprobably from PR #69391 It seems fix is quite trivial, I can do it.
|
Please feel free to submit a PR, just noting the fix may not be what is expected. With .NET 7 and the support for generic math Such a value is a Additional
|
There are some other methods where expressions with
I think all of them should be checked and fixed if needed. Question to the team: how to provide tests for such fixes? As I know such tests is too expensive to include them in the main pipeline. |
Almost all of these should be able to be handled centrally via the At a glance, it looks like the issue is that its doing The former gives us The latter gives us
We have a couple of 64-bit only tests, there isn't really a good way to do this outside of manual or outerloop testing due to the memory constrains, however. |
:) Almost the same limits are in JDK BigInteger - max length is limited to |
So, did I understand correctly that it is enough to simply reduce |
That'd be my expectation.
Theoretically someone could "break" in that an allocation that previously would've succeeded now fails. However, in practice this was already "flaky" since it is dependent on the machine having enough memory in the first place. This was part of the designed and intended changes we made in .NET 7 though, so it is "expected" to be failing and it not doing so is the bug at this point. |
Some random preliminary observations about the case:
|
- Limit `BigInteger.MaxLength` to `int.MaxValue/32` uints. This is equal to limit length in bits to `int.MaxValue-32 bits` - Fix ctor `BigInteger(ReadOnlySpan<byte>, bool, bool)` to check MaxLength limit - Fix private ctor `BigInteger(ReadOnlySpan<uint>, bool)` to check `MaxLength` limit after trying to conserve space, not before - Fix private ctor `BigInteger(Span<uint>)` to check MaxLength limit after trying to conserve space, not before - Fix obsolete comment in AssertValid() - Fix test `LargeNegativeBigIntegerShiftTest` to respect new limit. fix dotnet#84670
I've sent PR with basic implementation discussed before.
|
We can add the test but have it marked as 64-bit and explicit run only, for example:
It'd probably be useful to check as a fast path, but it isn't strictly needed particularly since the scenario should be very uncommon and will still ultimately fail.
Right.
I don't have a preference here.
A PR would be submitted to https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Numerics/BigInteger.xml updating the relevant documentation.
As long as we don't end up in a scenario where we end up with a "too large" big integer, we should be fine. Otherwise the API should be updated. Fast path checks to avoid unnecessary work aren't required, but are generally nice to have. |
@tannergooding did #102874 address this issue? |
Yes. |
Description
When bit length of BigInteger value is greater than
2^(int.MaxValue+1) - 1
, thenIBinaryInteger<BigInteger>.GetShortestBitLength()
can not return correct result because bit length is more than int.MaxValue.Now
IBinaryInteger<BigInteger>.GetShortestBitLength()
returns incorrect negative value.Reproduction Steps
Code:
Expected behavior
Exception on
GetShortestBitLength
call. Result can not be presented inint
type.Actual behavior
output
Regression?
No
Known Workarounds
No
Configuration
The latest
main
branchOther information
probably from PR #69391
cc @tannergooding
It seems fix is quite trivial, I can do it.
The text was updated successfully, but these errors were encountered: