Repository navigation
Remove unsafe code in CreateUriInfo and fix edge-case IndexOutOfRangeExceptions - #121653
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes issue #1487 by correcting how URI parsing handles strings with non-ASCII characters, specifically addressing an IndexOutOfRangeException that occurred when parsing implicit file URIs containing bidirectional control characters. The fix ensures that when the URI string is rebuilt to handle unicode characters, the correct string offsets are used throughout the parsing process.
Key changes:
- Fixed indexing bug in CreateUriInfo where
_string(potentially modified) was incorrectly used instead ofOriginalStringwhen tracking offsets, and vice versa when needed - Removed unsafe code from CreateUriInfo method by eliminating the
fixedstatement and pointer arithmetic - Added guard conditions in UriExt.cs to prevent parsing when exceptions have already occurred
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| UncTest.cs | Adds test coverage for UNC paths containing bidi control characters to prevent regression of #1487 |
| UriExt.cs | Adds null checks before hasUnicode parsing to avoid redundant work when errors already exist |
| Uri.cs | Refactors CreateUriInfo to remove unsafe code, fix string offset tracking between OriginalString and _string, and simplify control flow |
This was referenced Nov 15, 2025
Open
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
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.
Fixes #1487
Uri parsing happens in stages:
PrivateParseMinimal- The ctor will only do minimal validation to see whether it should throwCreateUriInfo- Accessing properties will allocateUriInfoand populate some offsets of various Uri components (scheme, userinfo, host, port). This is enough to access theHost.ParseRemaining- Accessing properties likePathAndQuerywill populate the rest of the offsets and check whether the input is in the canonical form (whether we'll have to do some text changes for the properties).If the input contains non-ASCII, we'll also rebuild the string representation and do all parts as part of the ctor. While recreating the string, we're dealing with two separate offsets (into the original and into the (partial) new string).
The issue with #1487 was that we were indexing into the new string instead of the original inside
CreateUriInfo, which broke implicit files where the host got reduced in length (by removing bidi chars).This PR fixes that, and also removes the use of unsafe code in that method.
No real perf impact - MihuBot/runtime-utils#1635, MihuBot/runtime-utils#1636
As alaways, easier to review without space changes.