Skip to content

Remove more unsafe code from Uri internals - #121270

Merged
MihaZupan merged 2 commits into
dotnet:mainfrom
MihaZupan:uri-unsafe-checkcanonical
Nov 24, 2025
Merged

MihaZupan merged 2 commits into
dotnet:mainfrom
MihaZupan:uri-unsafe-checkcanonical

Conversation

@MihaZupan

@MihaZupan MihaZupan commented Nov 1, 2025 •

Copy link
Copy Markdown
Member

Removes unsafe code from CheckCanonical and all its callers.

InternalIsWellFormedOriginalString was 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 IsAsciiLetterOrDigit in CheckCanonical, it's now faster - MihuBot/runtime-utils#1632. E.g.

Method Toolchain input Mean Error Ratio Allocated Alloc Ratio
CtorIdnHostPathAndQuery Main http://dot.ne(...)alue#fragment [43] 188.0 ns 1.44 ns 1.00 248 B 1.00
CtorIdnHostPathAndQuery PR http://dot.ne(...)alue#fragment [43] 160.6 ns 0.37 ns 0.85 248 B 1.00

@MihaZupan MihaZupan added this to the 11.0.0 milestone Nov 1, 2025
@MihaZupan MihaZupan self-assigned this Nov 1, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@MihaZupan

This comment was marked as outdated.

@MihuBot

This comment was marked as outdated.

@MihaZupan
MihaZupan force-pushed the uri-unsafe-checkcanonical branch from 4930cd3 to 316d51a Compare November 14, 2025 09:02
@MihaZupan
MihaZupan marked this pull request as ready for review November 14, 2025 11:00
Copilot AI review requested due to automatic review settings November 14, 2025 11:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CheckCanonical from unsafe pointer-based to safe ReadOnlySpan<char>-based implementation with an out int end parameter
  • Added Flags.SchemeNotCanonical_NoTrailingSlashes to 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

@MihaZupan
MihaZupan merged commit 0fe3eb1 into dotnet:main Nov 24, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants