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

Ensure that integer parsing correctly handles non-zero fractional data #106506

Merged
merged 1 commit into from
Aug 20, 2024
Merged
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 @@ -120,7 +120,7 @@ private static unsafe bool TryNumberBufferToBinaryInteger<TInteger>(ref NumberBu

int i = number.Scale;

if ((i > TInteger.MaxDigitCount) || (i < number.DigitsCount) || (!TInteger.IsSigned && number.IsNegative))
if ((i > TInteger.MaxDigitCount) || (i < number.DigitsCount) || (!TInteger.IsSigned && number.IsNegative) || number.HasNonZeroTail)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,46 @@ public static IEnumerable<object[]> Parse_Invalid_TestData()
yield return new object[] { "2147483649-", NumberStyles.AllowTrailingSign, null, typeof(OverflowException) };
yield return new object[] { "(2147483649)", NumberStyles.AllowParentheses, null, typeof(OverflowException) };
yield return new object[] { "2E10", NumberStyles.AllowExponent, null, typeof(OverflowException) };

// Test trailing non zeros

yield return new object[] { "-9223372036854775808.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting for reviewers that this Parse_Invalid_TestData is reused by the other integer types as part of their parsing validation logic.

For example:

foreach (object[] widerTests in Int32Tests.Parse_Invalid_TestData())
{
yield return widerTests;
}

and

foreach (object[] objs in Int32Tests.Parse_Invalid_TestData())
{
if ((Type)objs[3] == typeof(OverflowException) &&
(((NumberStyles)objs[1] & NumberStyles.AllowBinarySpecifier) != 0 || // TODO https://github.com/dotnet/runtime/issues/83619: Remove once BigInteger supports binary parsing
!BigInteger.TryParse((string)objs[0], (NumberStyles)objs[1], null, out BigInteger bi) ||
(bi >= long.MinValue && bi <= long.MaxValue)))
{
continue;
}
yield return objs;
}

yield return new object[] { "-2147483648.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "-32768.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "-128.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "127.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "255.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "32767.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "65535.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "2147483647.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "4294967295.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "9223372036854775807.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "18446744073709551615.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };

yield return new object[] { "-9223372036854775808.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "-2147483648.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "-32768.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "-128.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "127.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "255.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "32767.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "65535.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "2147483647.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "4294967295.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "9223372036854775807.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "18446744073709551615.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };

yield return new object[] { "3.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "3.000000001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "3.0000000001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "3.00000000001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };

yield return new object[] { "3.100", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "3.100000000", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "3.1000000000", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
yield return new object[] { "3.10000000000", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };

yield return new object[] { "2147483646.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
}

[Theory]
Expand Down
Loading