Skip to content

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

Description

@alimony

System.Reflection.Metadata.TypeName.Parse accepts U+0000 inside a type name and keeps it in FullName, but Type.GetType and Assembly.GetType resolve only the part before the NUL. So the two APIs disagree about which type a string names.

using System.Reflection.Metadata;

foreach (string name in new[] { "System.Int32\0junk", "System.Environment+SpecialFolder\0x" })
{
    TypeName parsed = TypeName.Parse(name);
    Type? loaded = Type.GetType(name);
    Console.WriteLine($"TypeName.FullName = {parsed.FullName.Replace("\0", "\\0")} | Type.GetType = {loaded?.FullName ?? "null"}");
}

Output on .NET 10.0.15:

TypeName.FullName = System.Int32\0junk | Type.GetType = System.Int32
TypeName.FullName = System.Environment+SpecialFolder\0x | Type.GetType = System.Environment+SpecialFolder

The same happens on .NET 9 and 11, for generic arguments (List1[[System.Int32\0zz]]) and with an assembly-qualified name (System.Diagnostics.Process\0x, System.Diagnostics.ProcessloadsSystem.Diagnostics.Process`).

The cause appears to be that CoreCLR passes the name to AssemblyNative_GetTypeCore as a NUL-terminated UTF-8 string (RuntimeAssembly.cs, StringMarshalling.Utf8), so everything after the NUL is dropped, while TypeNameParserHelpers.GetFullTypeNameLength treats \0 as an ordinary character.

The assembly-name parser already rejects it (AssemblyNameParser.TryGetNextChar returns false on '\0'), and "System.Int32, System.Private.CoreLib\0x" fails with "The given assembly name was invalid". Rejecting '\0' in GetFullTypeNameLength (or in NeedsEscaping) the same way would make TypeName.Parse fail on input that Type.GetType cannot resolve as written. That would close the gap for code that validates a name with TypeName and then loads it with Type.GetType.

Activity

  1. added
    untriaged
    on Nov 19, 2026
  2. dotnet-policy-service commented on Nov 19, 2026

    @dotnet-policy-service
    Contributor

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

  3. dotnet-policy-service commented on Nov 19, 2026

    @dotnet-policy-service
    Contributor

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

  4. dotnet-policy-service commented on Nov 19, 2026

    @dotnet-policy-service
    Contributor

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

  5. removed
    untriaged
    on Nov 24, 2026
  6. added a commit that references this issue on Nov 24, 2026
    b1b5306
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions