You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: (bug) Signed compare against a path-dependent checked bound eliminates a bounds check #133821
RangeCheck treats new int[n]'s length as a method-wide "checked bound" for the VN of n once any block indexes that array. On a path where the allocation never happened and n is negative, a signed k >= n compare is still consumed as a bound-relative fact, so the index is "proven" non-negative and the bounds check for arr[i] is removed.
Minimal Repro
usingSystem;usingSystem.Runtime.CompilerServices;publicclassProgram{staticint[]s_arr=newint[8];staticints_sink;[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.AggressiveOptimization)]staticintTest(int[]arr,intn,intk,boolcond){if(cond){int[]tmp=newint[n];s_sink=tmp[0];}inti=0;if(k>=n){i=k;}if(i<arr.Length){returnarr[i];}return-1;}staticintMain(){for(intj=0;j<3;j++){Test(s_arr,4,2,true);}try{Console.WriteLine("No exception, returned "+Test(s_arr,-5,-3,false));}catch(IndexOutOfRangeException){Console.WriteLine("IndexOutOfRangeException");}return100;}}
In the final call cond == false, k >= n is -3 >= -5, so i == -3 and -3 < 8 holds — arr[-3] must throw.
Expected
IndexOutOfRangeException
Actual
UB
The codegen emits mov eax, eax (zero-extend of a negative index) and no CORINFO_HELP_RNGCHKFAIL for the load; the only one left belongs to tmp[0]. DOTNET_JITMinOpts=1 is correct.
Notes
x64, main @ 6ed2ba318dcaa936da60f7d6bc9eb30cad54fc0c; also reproduces on .NET 10.0.12 Release, so it affects servicing branches (not bisected).
ARR_LENGTH(new int[n]) folds to the VN of n, so tmp[0] registers n method-wide as a checked bound $bnd.
On the cond == false path the signed k >= n yields a keBinOpArray($n, 0) lower limit for i, and Range::Merge's <$bnd + cns1, ...> U <cns2, ...> rule collapses it to the constant 0, making the upper-bound-only i < arr.Length check look sufficient.
Silent memory-safety hole: with a suitable length/index this is an arbitrary in-process OOB read rather than a crash.
changed the title [-]JIT (bug): Signed compare against a path-dependent checked bound eliminates a bounds check[/-][+]JIT: (bug) Signed compare against a path-dependent checked bound eliminates a bounds check[/+]on Sep 16, 2026
Activity
dotnet-policy-service commentedon Sep 13, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
[-]JIT (bug): Signed compare against a path-dependent checked bound eliminates a bounds check[/-][+]JIT: (bug) Signed compare against a path-dependent checked bound eliminates a bounds check[/+]EgorBo commentedon Sep 18, 2026
Affected SDK versions:
JIT: require nonnegative bounds for symbolic range limits (dotnet#135457