Skip to content

Commit b4d4333

Browse files
committed
Ensure that integer parsing correctly handles non-zero fractional data
1 parent a842025 commit b4d4333

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static unsafe bool TryNumberBufferToBinaryInteger<TInteger>(ref NumberBu
120120

121121
int i = number.Scale;
122122

123-
if ((i > TInteger.MaxDigitCount) || (i < number.DigitsCount) || (!TInteger.IsSigned && number.IsNegative))
123+
if ((i > TInteger.MaxDigitCount) || (i < number.DigitsCount) || (!TInteger.IsSigned && number.IsNegative) || number.HasNonZeroTail)
124124
{
125125
return false;
126126
}

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,46 @@ public static IEnumerable<object[]> Parse_Invalid_TestData()
663663
yield return new object[] { "2147483649-", NumberStyles.AllowTrailingSign, null, typeof(OverflowException) };
664664
yield return new object[] { "(2147483649)", NumberStyles.AllowParentheses, null, typeof(OverflowException) };
665665
yield return new object[] { "2E10", NumberStyles.AllowExponent, null, typeof(OverflowException) };
666+
667+
// Test trailing non zeros
668+
669+
yield return new object[] { "-9223372036854775808.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
670+
yield return new object[] { "-2147483648.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
671+
yield return new object[] { "-32768.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
672+
yield return new object[] { "-128.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
673+
yield return new object[] { "127.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
674+
yield return new object[] { "255.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
675+
yield return new object[] { "32767.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
676+
yield return new object[] { "65535.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
677+
yield return new object[] { "2147483647.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
678+
yield return new object[] { "4294967295.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
679+
yield return new object[] { "9223372036854775807.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
680+
yield return new object[] { "18446744073709551615.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
681+
682+
yield return new object[] { "-9223372036854775808.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
683+
yield return new object[] { "-2147483648.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
684+
yield return new object[] { "-32768.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
685+
yield return new object[] { "-128.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
686+
yield return new object[] { "127.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
687+
yield return new object[] { "255.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
688+
yield return new object[] { "32767.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
689+
yield return new object[] { "65535.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
690+
yield return new object[] { "2147483647.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
691+
yield return new object[] { "4294967295.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
692+
yield return new object[] { "9223372036854775807.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
693+
yield return new object[] { "18446744073709551615.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
694+
695+
yield return new object[] { "3.001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
696+
yield return new object[] { "3.000000001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
697+
yield return new object[] { "3.0000000001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
698+
yield return new object[] { "3.00000000001", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
699+
700+
yield return new object[] { "3.100", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
701+
yield return new object[] { "3.100000000", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
702+
yield return new object[] { "3.1000000000", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
703+
yield return new object[] { "3.10000000000", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
704+
705+
yield return new object[] { "2147483646.1", NumberStyles.Number, CultureInfo.InvariantCulture, typeof(OverflowException) };
666706
}
667707

668708
[Theory]

0 commit comments

Comments
 (0)