Fix Uri.GetHashCode hash/equality contract violation for file: URIs#124652
Merged
Fix Uri.GetHashCode hash/equality contract violation for file: URIs#124652
Conversation
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
…just IsUncOrDosPath Co-authored-by: MihaZupan <[email protected]>
Copilot
AI
changed the title
[WIP] Fix Uri GetHashCode mismatch for equal instances
Fix Uri.GetHashCode hash/equality contract violation for file: URIs
Feb 20, 2026
This was referenced Feb 20, 2026
MihaZupan
approved these changes
Feb 21, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a hash/equals contract violation in Uri.GetHashCode for file: URIs. The issue occurs because GetHashCode was using IsUncOrDosPath to determine whether to use case-insensitive hashing, while Equals can return true for two file: URIs where one has IsUncOrDosPath=false (plain file:/// URI) and the other has IsUncOrDosPath=true (UNC-derived URI). Since IsUncOrDosPath only covers a subset of file: URIs, two equal file: URIs could produce different hash codes.
Changes:
- Changed
Uri.GetHashCode()from usingIsUncOrDosPathtoIsFilewhen selecting hash algorithm - Added test case verifying UNC-derived file: URI equals plain file: URI with consistent hash codes
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.Uri/src/System/Uri.cs | Fixed GetHashCode to use IsFile instead of IsUncOrDosPath for selecting case-insensitive hashing |
| src/libraries/System.Private.Uri/tests/FunctionalTests/UriMethodsTests.cs | Added test case comparing UNC path \\\\\u202a with file:/// to verify hash/equals contract |
wfurt
approved these changes
Feb 22, 2026
wfurt
approved these changes
Feb 22, 2026
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.
Uri.GetHashCodeusesIsUncOrDosPathto select betweenOrdinalIgnoreCaseand ordinal hashing, butEqualsusesthis.IsUncOrDosPathfor comparison mode — so two URIs with identical normalizedRemoteUrlbut differentIsUncOrDosPathflags (e.g. a UNC-derived URI and an explicitfile:///) compare equal yet produce different hash codes.Description
Root cause:
Flags.DosPathandFlags.UncPathare only ever set alongsidesyntax = UriParser.FileUri, soIsUncOrDosPath ⇒ IsFileis a provable invariant. A plainfile:///URI never sets those flags, soGetHashCodeused a different hash algorithm than a UNC-derived URI with the same normalized form.Fix: Replace
if (IsUncOrDosPath)withif (IsFile)inGetHashCode. This extendsOrdinalIgnoreCasehashing to allfile:URIs, ensuring consistent hash codes for any twofile:URIs thatEqualscan report as equal. No existing behavior changes for UNC/DOS paths; the only difference is that plainfile:URIs now also useOrdinalIgnoreCase.GetHashCode.Test: Added
new Uri("\\\\\u202a")vsnew Uri("file:///")toEquals_TestData, which is already consumed byEqualsTest— a harness that automatically verifies both equality symmetry and hash code consistency for equal pairs.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.