From ca8c21b27a1b472dca7c3f5113b662fcbb4f74c3 Mon Sep 17 00:00:00 2001 From: Jerome Haltom Date: Sun, 12 Jul 2026 23:16:36 -0500 Subject: [PATCH] Fix nested annotation element encoding in StubGenerator The object-overload of EncodeElementValue iterated a nested annotation's name/value pairs with i++ instead of i += 2. After the first pair, i lands on a value slot, so (string)v[i] throws InvalidCastException when the value is an array (nested annotation, enum, or array member). This was a porting typo introduced in 3dad0f9dc when StubGen was rewritten on top of IKVM.ByteCode; the pre-rewrite loop and the two sibling loops (EncodeAnnotation and the CustomAttributeTypedArgument overload) all use i += 2 correctly. Fixes #724 --- src/IKVM.Runtime/StubGen/StubGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IKVM.Runtime/StubGen/StubGenerator.cs b/src/IKVM.Runtime/StubGen/StubGenerator.cs index c50402c562..2a6e36b572 100644 --- a/src/IKVM.Runtime/StubGen/StubGenerator.cs +++ b/src/IKVM.Runtime/StubGen/StubGenerator.cs @@ -965,7 +965,7 @@ void EncodeElementValue(ClassFileBuilder builder, ref ElementValueEncoder encode { e.Annotation(builder.Constants.GetOrAddUtf8(DecodeTypeName((string)v[1])), e2 => { - for (int i = 2; i < v.Length; i++) + for (int i = 2; i < v.Length; i += 2) e2.Element(builder.Constants.GetOrAddUtf8((string)v[i]), e3 => EncodeElementValue(builder, ref e3, v[i + 1])); }); });