Skip to content

Reject NUL characters in Metadata TypeName parsing - #137922

Merged
jkotas merged 6 commits into
mainfrom
copilot/fix-typename-parse-nul-handling
Nov 24, 2026
Merged

jkotas merged 6 commits into
mainfrom
copilot/fix-typename-parse-nul-handling

Conversation

@jkotas

@jkotas jkotas commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

TypeName.Parse preserves embedded NULs, while runtime type loading can truncate names at them. This allows validation and loading to interpret the same string differently.

  • Validation: Reject NULs anywhere in the input: Parse throws ArgumentException; TryParse returns false.
  • Compatibility: Scope the check to Metadata, leaving shared CoreLib parsing unchanged.
  • Coverage: Add cases for simple, nested, generic, assembly-qualified, and escaped names.

@azure-pipelines

azure-pipelines Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkotas jkotas changed the title [WIP] Fix TypeName.Parse handling of NUL character Reject NUL characters in Metadata TypeName parsing Nov 19, 2026
@jkotas
jkotas requested a balanced review from Copilot November 24, 2026 04:32

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.

🟡 Changes recommended

The new theory fails on Mono because its native NUL-terminated parser resolves "System.Int32\0junk" rather than returning null, so it needs an IsNotMonoRuntime condition or a corresponding Mono parser fix.

1 open issue
What changed in this PR

This PR addresses the mismatch between metadata type-name validation and runtime loading by rejecting embedded NUL characters in the shared type-name parser.

Changes:

  • Treat NUL as a parsing delimiter and reject escaped NUL characters.
  • Preserve NUL characters when constructing simple metadata type names.
  • Add parser and runtime-loading regression cases for simple, nested, generic, assembly-qualified, and escaped names.
File Description
src/​libraries/​System.Runtime/​tests/​System.Reflection.Tests/​GetTypeTests.cs Adds NUL rejection tests for runtime type loading.
src/​libraries/​System.Reflection.Metadata/​tests/​Metadata/​TypeNameTests.cs Tests parsing rejection and preservation during simple-name construction.
src/​libraries/​System.Reflection.Metadata/​tests/​Metadata/​TypeNameParserHelpersTests.cs Tests delimiter offsets and escaped-NUL rejection.
src/​libraries/​System.Reflection.Metadata/​src/​System/​Reflection/​Metadata/​TypeNameParserHelpers.cs Updates shared parsing while preserving NUL during escaping.

Review effort: Balanced

@jkotas
jkotas marked this pull request as ready for review November 24, 2026 04:56
@jkotas

jkotas commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

/ba-g dead letter + #138194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeName.Parse accepts '\0' in a type name, while Type.GetType truncates the name at it

4 participants