Skip to content

JIT: (bug) Enum.Equals folding throws NullReferenceException instead of returning false for a null argument #133817

Closed

Description

@EgorBo

Compiler::gtFoldExprCall folds NI_System_Enum_Equals into a direct comparison of the two boxed payloads without checking that the argument is non-null, so a null argument is unconditionally dereferenced. ValueType.Equals(object) must return false for null, never throw.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

enum E : int { A = 0, B = 1 }

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool Test(E x, E? y)
    {
        object a = x;
        object b = y; // null when y is null, but the JIT still sees exact class E
        return a.Equals(b);
    }

    static void Main() => Console.WriteLine(Test(E.A, null));
}

Expected

False

Actual

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Program.Test(E x, Nullable`1 y)

The generated code has the null path (xor rax, rax) flowing straight into cmp ecx, dword ptr [rax+0x08].

Notes

The fold requires only an exact class handle for both operands and explicitly discards the isNonNull result (bool isNonNull; // Unused here.). Boxing a Nullable<TEnum> yields either a boxed TEnum (exact handle) or null, so exactness does not imply non-null. FullOpts only; MinOpts/tier-0 are correct. Does not repro on .NET 10 — regression from d3f4519 ("Intrinsify Enum.Equals to avoid boxing", #122779).

Activity

  1. added this to the 12.0.0 milestone on Sep 13, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 13, 2026
  3. dotnet-policy-service commented on Sep 13, 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. changed the title [-]JIT (bug): `Enum.Equals` folding throws `NullReferenceException` instead of returning `false` for a `null` argument[/-] [+]JIT: (bug) `Enum.Equals` folding throws `NullReferenceException` instead of returning `false` for a `null` argument[/+] on Sep 16, 2026
  5. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 11.0.100-rc.2.26467.112
  6. added a commit that references this issue on Sep 24, 2026
    ddbf2d9
  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