Skip to content
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

BigInteger.Parse creates invalid instances from hex strings #74758

Closed
Tracked by #79004
adetunji-david opened this issue Aug 29, 2022 · 3 comments · Fixed by #84792
Closed
Tracked by #79004

BigInteger.Parse creates invalid instances from hex strings #74758

adetunji-david opened this issue Aug 29, 2022 · 3 comments · Fixed by #84792

Comments

@adetunji-david
Copy link

Description

By design, the following expression creates an instance of BigInteger with the value of -2
var x = BigInteger.Parse("FFFFFFFFE", NumberStyles.HexNumber);
However, x is not equal to another instance with a value of -2.

Reproduction Steps

var x = BigInteger.Parse("FFFFFFFFE", NumberStyles.HexNumber);
var minusTwo = new BigInteger(-2);
Console.WriteLine(x);
Console.WriteLine(x == minusTwo);
Console.WriteLine(x == -2);

Expected behavior

Output is
-2
True
True

Actual behavior

Output is
-2
True
False

Regression?

No response

Known Workarounds

No response

Configuration

.Net version: 6.0.300
Operating System: Microsoft Windows 10 Pro
Architecture: x64

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 29, 2022
@ghost
Copy link

ghost commented Aug 29, 2022

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

By design, the following expression creates an instance of BigInteger with the value of -2
var x = BigInteger.Parse("FFFFFFFFE", NumberStyles.HexNumber);
However, x is not equal to another instance with a value of -2.

Reproduction Steps

var x = BigInteger.Parse("FFFFFFFFE", NumberStyles.HexNumber);
var minusTwo = new BigInteger(-2);
Console.WriteLine(x);
Console.WriteLine(x == minusTwo);
Console.WriteLine(x == -2);

Expected behavior

Output is
-2
True
True

Actual behavior

Output is
-2
True
False

Regression?

No response

Known Workarounds

No response

Configuration

.Net version: 6.0.300
Operating System: Microsoft Windows 10 Pro
Architecture: x64

Other information

No response

Author: adetunji-david
Assignees: -
Labels:

area-System.Numerics

Milestone: -

@tannergooding
Copy link
Member

tannergooding commented Aug 29, 2022

This bug also repro's in .NET 6. Parse is creating a BigInteger with bits = { 0, 2 }, sign = -1

Where-as casting and implicit conversion creates a BigInteger with bits = null, sign = -2

@tannergooding tannergooding added bug and removed untriaged New issue has not been triaged by the area owner labels Aug 29, 2022
@tannergooding tannergooding added this to the 8.0.0 milestone Aug 29, 2022
@speshuric
Copy link
Contributor

It seems I found the cause of the bug. I've fixed it locally, but it needs to be tested. I think I'll send PR in a couple of days.

speshuric added a commit to speshuric/runtime that referenced this issue Apr 13, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jun 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants