Skip to content

JIT: (bug) comparing GetType() of two references drops the null check on a null reference #133559

Closed

Description

@EgorBo

Repro

using System;
using System.Runtime.CompilerServices;

internal static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    private static int Test(string a, string b)
    {
        if (a.GetType() != b.GetType())
            return 2;

        return 3;
    }

    private static void Main()
    {
        try
        {
            Console.WriteLine($"returned {Test(null, "text")}");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("NullReferenceException");
        }
    }
}

Expected

NullReferenceException

Actual

returned 3

a is null, so a.GetType() must throw, but the whole method body is optimized away to
return 3.

Platform

Windows x64, local Checked build of dotnet/runtime main (ef97eb5).

Activity

  1. added this to the 12.0.0 milestone on Sep 10, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 10, 2026
  3. dotnet-policy-service commented on Sep 10, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. self-assigned this
    on Sep 10, 2026
  5. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  6. added a commit that references this issue on Sep 24, 2026
    365b785
  7. locked and limited conversation to collaborators on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions