Reject non-numeric input in Length.TryParse - #195
Merged
TylerBrinks merged 1 commit intoJul 23, 2026
Merged
Conversation
Length.TryParse falls back to returning Zero whenever the parsed value
is 0, to honour "a zero length may omit its unit". But
StringExtensions.StylesheetUnit sets its out parameter to 0 both when it
parses a unitless zero and when it fails to find a number at all, so the
fallback also fired for input that is not a length:
Length.TryParse("auto", out var r) // true, r == 0px
Length.TryParse("red", out var r) // true, r == 0px
Length.TryParse("", out var r) // true, r == 0px
The two cases are distinguishable by the returned unit string: empty for
a plain number, null when nothing could be tokenized. Check it as well
as the value.
Angle, Frequency and Resolution have no equivalent fallback - they
require a recognised unit - so this is specific to Length.
jhaygood86
marked this pull request as ready for review
July 22, 2026 21:27
jhaygood86
marked this pull request as draft
July 22, 2026 21:40
jhaygood86
marked this pull request as ready for review
July 22, 2026 21:44
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.
Problem
Length.TryParsereports success for input that is not a length at all:The zero fallback exists for a good reason — CSS Values 3 §5.2 allows a zero length to omit its unit — but it keys only on the value:
StringExtensions.StylesheetUnitsetsresult = default(0) both when it parses a unitless zero and when it can't find a number at all, so the two are indistinguishable by value alone.Fix
They are distinguishable by the returned unit string — empty for a plain number,
nullwhen nothing could be tokenized — so check that too.Angle,FrequencyandResolutionhave no equivalent fallback (they require a recognised unit), so this is specific toLength.Tests
New
LengthTests.cs, 19 theory cases:0pxand negatives0,0.0,-0"",auto,inherit,red,px,%,garbage5,-2.5) and an unrecognised unit (10foo)7 fail on
master. The full suite (1263 existing tests) stays green, and all seven target frameworks build with no new warnings.