Skip to content

Commit 5968a6f

Browse files
[release/10.0] Fix off-by-one error in TypePreinit switch instruction handling (#127587)
Backport of #123911 to release/10.0 /cc @agocke @sbomer ## Customer Impact - [x] Customer reported - [ ] Found internally When the value exactly equals the case count, the code tried to read a non-existent jump table entry, corrupting the IL reader offset and causing an IndexOutOfRangeException during NativeAOT compilation. ## Regression - [ ] Yes - [X] No ## Testing Unit tests ## Risk Low Co-authored-by: Sven Boemer <sbomer@gmail.com>
1 parent fc46e69 commit 5968a6f

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ private Status TryScanMethod(MethodIL methodIL, Value[] parameters, Stack<Method
12721272

12731273
uint count = reader.ReadILUInt32();
12741274
int nextInstruction = reader.Offset + (int)(4 * count);
1275-
if (target > count)
1275+
if (target >= count)
12761276
{
12771277
reader.Seek(nextInstruction);
12781278
}

src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ class Switcher
813813
public static int CaseMinus1 = Switch(-1);
814814
public static int Case0 = Switch(0);
815815
public static int Case6 = Switch(6);
816+
public static int Case7 = Switch(7); // Boundary: value == case count (tests fix for https://github.com/dotnet/runtime/issues/123833)
816817
public static int Case100 = Switch(100);
817818

818819
private static int Switch(int x)
@@ -837,6 +838,7 @@ public static void Run()
837838
Assert.AreEqual(Switcher.CaseMinus1, 100000);
838839
Assert.AreEqual(Switcher.Case0, 100);
839840
Assert.AreEqual(Switcher.Case6, 700);
841+
Assert.AreEqual(Switcher.Case7, 100000);
840842
Assert.AreEqual(Switcher.Case100, 100000);
841843
}
842844
}

0 commit comments

Comments
 (0)