Skip to content

Commit 34b8e6f

Browse files
Update error messages and fix int.MinValue overflow in FormatInt32ToValueListBuilder
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
1 parent f5d2798 commit 34b8e6f

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/libraries/System.Private.CoreLib/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@
409409
<data name="Arg_HandleNotSync" xml:space="preserve">
410410
<value>Handle does not support synchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened asynchronously (that is, it was opened explicitly for overlapped I/O).</value>
411411
</data>
412+
<data name="Arg_BinaryStyleNotSupported" xml:space="preserve">
413+
<value>The number style AllowBinarySpecifier is not supported on floating point data types.</value>
414+
</data>
412415
<data name="Arg_HexBinaryStylesNotSupported" xml:space="preserve">
413416
<value>The number styles AllowHexSpecifier and AllowBinarySpecifier are not supported on floating point data types.</value>
414417
</data>

src/libraries/System.Private.CoreLib/src/System/Globalization/NumberFormatInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ internal static void ValidateParseStyleFloatingPoint(NumberStyles style)
837837
// Binary specifier is not supported for floating point
838838
if ((style & NumberStyles.AllowBinarySpecifier) != 0)
839839
{
840-
ThrowHexBinaryStylesNotSupported();
840+
ThrowBinaryStyleNotSupported();
841841
}
842842

843843
// When AllowHexSpecifier is used, only specific flags are allowed
@@ -856,8 +856,8 @@ static void ThrowInvalidStyle() =>
856856
throw new ArgumentException(SR.Argument_InvalidNumberStyles, nameof(style));
857857

858858
[DoesNotReturn]
859-
static void ThrowHexBinaryStylesNotSupported() =>
860-
throw new ArgumentException(SR.Arg_HexBinaryStylesNotSupported, nameof(style));
859+
static void ThrowBinaryStyleNotSupported() =>
860+
throw new ArgumentException(SR.Arg_BinaryStyleNotSupported, nameof(style));
861861

862862
[DoesNotReturn]
863863
static void ThrowInvalidHexBinaryStyle() =>

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,22 +647,28 @@ private static void FormatFloatAsHex<TNumber, TChar>(ref ValueListBuilder<TChar>
647647
private static void FormatInt32ToValueListBuilder<TChar>(ref ValueListBuilder<TChar> vlb, int value)
648648
where TChar : unmanaged, IUtfChar<TChar>
649649
{
650+
uint uvalue;
650651
if (value < 0)
651652
{
652653
vlb.Append(TChar.CastFrom('-'));
653-
value = -value;
654+
// Handle int.MinValue overflow: negating int.MinValue would overflow
655+
// Use unchecked cast to uint which handles the two's complement correctly
656+
uvalue = unchecked((uint)-value);
657+
}
658+
else
659+
{
660+
uvalue = (uint)value;
654661
}
655662

656663
// Handle zero specially
657-
if (value == 0)
664+
if (uvalue == 0)
658665
{
659666
vlb.Append(TChar.CastFrom('0'));
660667
return;
661668
}
662669

663670
// Format digits in reverse, then reverse the span
664671
int startIndex = vlb.Length;
665-
uint uvalue = (uint)value;
666672

667673
while (uvalue > 0)
668674
{

0 commit comments

Comments
 (0)