Skip to content

Missing IndexOutOfRangeException in optimized code #133271

Description

@EgorBo
usingSystem;usingSystem.Runtime.CompilerServices;Console.WriteLine(Test(newint[4],int.MinValue));[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.AggressiveOptimization)]staticintTest(int[]array,intindex){if((uint)(index-int.MaxValue)<(uint)array.Length&&index<array.Length){refintunused=refarray[index];return1;}return0;}

Optimized: prints 1.
With DOTNET_JitMinOpts=1: throws IndexOutOfRangeException (expected).

.NET 11.0, win-x64.

unchecked(int.MinValue - int.MaxValue) is 1, so both guards pass, but index is still negative. The negative-offset case in MergeEdgeAssertionsWorker incorrectly infers index >= int.MaxValue and the bounds check is removed.

Copilot-generated.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions