Repository navigation
Reject hex and binary NumberStyles in Parse and TryParse [patch] - #179
Merged
Merged
Conversation
Parse and TryParse never looked at AllowHexSpecifier or AllowBinarySpecifier,
so Parse("10", NumberStyles.HexNumber) read the digits as decimal and returned
10, with no error. Both styles now throw ArgumentException, as decimal and
double do, from Parse and TryParse alike.
Fixes #177
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5ShapPU8BXcXvikJm
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #177
What was wrong
Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?)never readAllowHexSpecifierorAllowBinarySpecifier. Input made only of decimal digits was read as decimal, soT.Parse("10", NumberStyles.HexNumber, …)returned 10 forPreciseNumberwith no error.BigIntegerreturns 16 for the same call, anddecimalanddoublethrow.The fix
The issue offered two options. This PR takes the one the triage recommended: refuse both styles with an
ArgumentException, asdecimalanddoubledo. The check runs at the top ofParse.TryParsecatches onlyFormatExceptionandOverflowException, so it throws the same exception rather than returning false, which also matchesdecimal.TryParse.NumberStyles.AllowBinarySpecifieronly exists from .NET 8 onward. On thenet7.0target the mask uses its raw value (0x400), so a caller there who passes it is still refused.Tests
ParseAndTryParse_RejectHexAndBinaryStylesAsDecimalDoescoversHexNumber,AllowHexSpecifier,BinaryNumberandAllowBinarySpecifier. For each one it checks thatdecimal.Parsethrows, then asserts thatPreciseNumber's string and spanParse, its string and spanTryParse, and a genericINumberBase<T>.Parseall throwArgumentException."A"throwsFormatException. With the fix they pass.Generated by Claude Code