Skip to content

JIT: (bug) multi-dimensional array Set checks bounds/null before evaluating the stored value #133717

Closed

Description

@EgorBo
using System;
using System.Runtime.CompilerServices;

class P
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static void Test(int[,] a, int i, int d)
    {
        a[i, 0] = 100 / d;   // == a.Set(i, 0, 100 / d): the RHS argument must be evaluated first
    }

    static void Main()
    {
        try { Test(new int[2, 2], 5, 0); } catch (Exception e) { Console.WriteLine(e.GetType().Name); }
        try { Test(null, 0, 0); } catch (Exception e) { Console.WriteLine(e.GetType().Name); }
    }
}

Expected: DivideByZeroException twice — 100 / d is an argument of the int[,]::Set call and must be evaluated before Set runs its checks.
Actual: IndexOutOfRangeException then NullReferenceException.

DOTNET_JITMinOpts=1 prints the correct DivideByZeroException twice; full opts (shown above, or DOTNET_TieredCompilation=0) is wrong.
src\coreclr\jit\importercalls.cpp impArrayAccessIntrinsic / NI_Array_Set pops the value into the store's RHS without spilling it, unlike the SZ-array stelem path (importer.cpp:7558).
Also reproduces for rank 3 (int[,,]).

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 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 11, 2026
  5. added a commit that references this issue on Sep 14, 2026
    3a046d6
  6. added a commit that references this issue on Sep 18, 2026
    826ce06
  7. 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
  8. locked and limited conversation to collaborators on Oct 19, 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