Repository navigation
[patch] Reject non-hex characters in Color.FromHex with ArgumentException - #399
Merged
Merged
Conversation
…tion Fixes #313 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019RYN9jT6szUiySkywFKCA1
|
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 #313
What was wrong
Color.FromHexchecked only the string's length, then parsed each channel withConvert.ToByte(s, 16). That call has two quirks:+, so#+F+F+Fwas silently accepted as#0F0F0F.FormatExceptionfor most other bad characters (#GGGGGG,#0x0x0x) andArgumentExceptionfor-. The exception type depended on which bad character showed up, and the docs promise onlyArgumentException.Change (
Semantics.Color/Color.Conversions.cs)0-9,A-Fora-f. Anything else throwsArgumentException(..., nameof(hex)), which names the bad character.Convert.ToBytenow only ever sees hex digits, so it can no longer accept a sign or throwFormatException. I kept it rather than switching tobyte.Parse(ReadOnlySpan<char>, …)because that overload isn't available on thenetstandard2.0target.<exception>doc to cover non-hex characters.Tests (
HexConversionTests)FromHex_NonHexCharacter_ThrowsArgumentException:#+F+F+F,+F+F+F,#00+F00,#GGGGGG,#0x0x0x,#-1-1-1,#12345G,#FFFFFF+F,#F F, and#G00all throw exactlyArgumentException.FromHex_ValidDigitsInEitherCase_RoundTrip: valid 3, 6 and 8-digit inputs in lower and upper case, with and without#, still round-trip.With the fix reverted, 9 of these cases fail. With the fix, the full
Semantics.Testsuite passes: 1495 passed, 0 failed, 8 skipped.Semantics.Colorbuilds for every target framework.🤖 Generated with Claude Code
https://claude.ai/code/session_019RYN9jT6szUiySkywFKCA1
Generated by Claude Code