dotnet / dotnet/runtime

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

Open
#133,559 1 comment 0 reactions 1 assignee Claimed by @EgorBo View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

## Repro

```csharp
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 (ef97eb54da2).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.