Handle null FieldName in FieldIdentifier.GetHashCode#66409
Open
MohamedOthman1 wants to merge 2 commits intodotnet:mainfrom
Open
Handle null FieldName in FieldIdentifier.GetHashCode#66409MohamedOthman1 wants to merge 2 commits intodotnet:mainfrom
MohamedOthman1 wants to merge 2 commits intodotnet:mainfrom
Conversation
The public constructor rejects null fieldName, but `default(FieldIdentifier)` bypasses it and leaves both Model and FieldName as null. Calling GetHashCode on that default value invoked StringComparer.Ordinal.GetHashCode(null), which throws ArgumentNullException with the message "Value cannot be null. (Parameter 'obj')" — surfaced by callers such as EditContext.NotifyFieldChanged. Guard against the null FieldName so dictionary lookups and Equals comparisons work for default-valued instances. Model is already safe because RuntimeHelpers.GetHashCode returns 0 for null. Fixes dotnet#45096
Contributor
|
Thanks for your PR, @MohamedOthman1. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a crash when default(FieldIdentifier) is used as a dictionary key (or otherwise hashed) by making FieldIdentifier.GetHashCode() resilient to a null FieldName, which can occur when the struct is default-initialized (bypassing the constructor).
Changes:
- Guard
FieldIdentifier.GetHashCode()againstFieldName == nullto preventArgumentNullException. - Add regression tests ensuring default
FieldIdentifierhashing/equality works and it can be used as a dictionary key.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Components/Forms/src/FieldIdentifier.cs | Prevents GetHashCode() from throwing when FieldName is null on a default struct value. |
| src/Components/Forms/test/FieldIdentifierTest.cs | Adds regression coverage for hashing/equality and dictionary usage of default(FieldIdentifier). |
Author
|
@dotnet-policy-service agree |
This was referenced Apr 22, 2026
Open
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.
Summary
Fixes #45096.
FieldIdentifieris areadonly structwhose public constructor rejectsnullforfieldName. However,default(FieldIdentifier)bypasses the constructor and leavesModelandFieldNameasnull. CallingGetHashCode()on that default value invokesStringComparer.Ordinal.GetHashCode(null), which throws:This surfaces via any caller that hashes a default-valued
FieldIdentifier— most commonlyEditContext.NotifyFieldChanged, which uses aDictionary<FieldIdentifier, FieldState>keyed on the identifier. The repro in the issue hits this when a component callsEditContext.NotifyFieldChanged(FieldIdentifier)before itsValueExpressionhas been assigned.Fix
Guard
FieldIdentifier.GetHashCode()against a nullFieldName.Modelis already safe becauseRuntimeHelpers.GetHashCode(null)returns0and does not throw.Equalsalso already handles null viastring.Equals(..., StringComparison.Ordinal), so no change is needed there.This is a root-cause fix at the struct level, so every consumer (
NotifyFieldChanged,MarkAsUnmodified,GetValidationMessages(FieldIdentifier),IsValid(FieldIdentifier), etc.) benefits automatically — rather than adding a guard at each public entry point.Tests
Added three tests to
FieldIdentifierTestcoveringdefault(FieldIdentifier):DefaultFieldIdentifier_GetHashCode_DoesNotThrow— regression test against the original crash.DefaultFieldIdentifiers_AreEqual— verifies consistent equality / hash between two default values.DefaultFieldIdentifier_CanBeUsedAsDictionaryKey— mirrors the real-worldEditContextusage.Full
Microsoft.AspNetCore.Components.Forms.Testssuite: 81 passed, 0 failed locally.