Repository navigation
Remove more unsafe code from Uri internals - #121270
Merged
Merged
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/ncl |
MihaZupan
commented
Nov 1, 2025
This comment was marked as outdated.
This comment was marked as outdated.
MihaZupan
commented
Nov 3, 2025
This comment was marked as outdated.
This comment was marked as outdated.
…tString, InternalIsWellFormedOriginalString
MihaZupan
force-pushed
the
uri-unsafe-checkcanonical
branch
from
November 14, 2025 09:02
4930cd3 to
316d51a
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR removes unsafe code from Uri internals by refactoring CheckCanonical and all its callers to work with ReadOnlySpan<char> instead of unsafe char* pointers. A new flag SchemeNotCanonical_NoTrailingSlashes is introduced to avoid duplicate validation during parsing. The refactoring also adds a fast path for ASCII letters and digits in CheckCanonical, improving performance.
Key changes:
- Refactored
CheckCanonicalfromunsafepointer-based to safeReadOnlySpan<char>-based implementation with anout int endparameter - Added
Flags.SchemeNotCanonical_NoTrailingSlashesto track schemes with authority but without proper://delimiter - Enhanced test coverage with scheme validation tests and surrogate pair tests
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| UriIsWellFormedUriStringTest.cs | Adds test cases for malformed schemes (missing/wrong slashes), reorganizes existing test, adds surrogate pair test |
| UriExt.cs | Refactors InternalIsWellFormedOriginalString to use new safe CheckCanonical API and new flag |
| Uri.cs | Refactors CheckCanonical to safe span-based code, updates all callers (CreateHostString, ParseRemaining), adds new flag |
This was referenced Nov 14, 2025
EgorBo
approved these changes
Nov 24, 2025
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Removes unsafe code from
CheckCanonicaland all its callers.InternalIsWellFormedOriginalStringwas also checking the same thing twice, and we also check for it during parsing, so I added a new flag for it to avoid the duplication (SchemeNotCanonical_NoTrailingSlashes).Reviewing is easier without whitespace diffs.
Due to the new fast path check for
IsAsciiLetterOrDigitinCheckCanonical, it's now faster - MihuBot/runtime-utils#1632. E.g.