Skip to content

Fix assertion at System.Numerics.BigInteger.Parse #96746

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,12 @@ internal static ParsingStatus TryParseBigIntegerBinaryNumberStyle(ReadOnlySpan<c

Debug.Assert(partialDigitCount == 0 && bufferPos == -1);

if (isNegative)
{
NumericsHelpers.DangerousMakeTwosComplement(buffer);
}

// BigInteger requires leading zero blocks to be truncated.
buffer = buffer.TrimEnd(0u);

int sign;
Expand All @@ -612,26 +618,15 @@ internal static ParsingStatus TryParseBigIntegerBinaryNumberStyle(ReadOnlySpan<c
sign = 0;
bits = null;
}
else if (buffer.Length == 1)
else if (buffer.Length == 1 && buffer[0] <= int.MaxValue)
{
sign = (int)buffer[0];
sign = (int)buffer[0] * (isNegative ? -1 : 1);
bits = null;

if ((!isNegative && sign < 0) || sign == int.MinValue)
{
bits = new[] { (uint)sign };
sign = isNegative ? -1 : 1;
}
}
else
{
sign = isNegative ? -1 : 1;
bits = buffer.ToArray();

if (isNegative)
{
NumericsHelpers.DangerousMakeTwosComplement(bits);
}
}

result = new BigInteger(sign, bits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,11 @@ public static bool TryParse([NotNullWhen(true)] string? value, out BigInteger re

public static bool TryParse([NotNullWhen(true)] string? value, NumberStyles style, IFormatProvider? provider, out BigInteger result)
{
if (value is null)
{
result = 0;
return false;
}
return TryParse(value.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result);
}

Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Runtime.Numerics/tests/BigInteger/parse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ void Test()
[Fact]
public void Parse_Hex32Bits()
{
// SimpleNumbers - 0xF
for (int i = 1; i < 2 * BigInteger.kcbitUint + 2; i++)
{
VerifyParseToString(new string('F', i), NumberStyles.HexNumber, true);
}

// Regression test for: https://github.com/dotnet/runtime/issues/54251
BigInteger result;

Expand Down Expand Up @@ -342,6 +348,18 @@ private static void VerifyBinaryNumberStyles(NumberStyles ns, Random random)
VerifyParseToString("1", ns, true);
VerifyParseToString("001", ns, true);

// SimpleNumbers - Zero
for (int i = 1; i < 2 * BigInteger.kcbitUint + 2; i++)
{
VerifyParseToString(new string('0', i), ns, true);
}

// SimpleNumbers - One
for (int i = 1; i < 2 * BigInteger.kcbitUint + 2; i++)
{
VerifyParseToString(new string('1', i), ns, true);
}

// SimpleNumbers - Small
for (int i = 0; i < s_samples; i++)
{
Expand Down