Skip to content

Commit f267a78

Browse files
Fix #855 - ArgumentOutOfRangeException in TransformCollectionAndObjectInitializers.DoTransform
1 parent 8285d01 commit f267a78

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

ICSharpCode.Decompiler.Tests/TestCases/Correctness/InitializerTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,5 +1015,35 @@ public static void Bug270_NestedInitialisers()
10151015
}
10161016
};
10171017
}
1018+
1019+
class Issue855
1020+
{
1021+
class Data
1022+
{
1023+
public object Obj;
1024+
}
1025+
1026+
class Items
1027+
{
1028+
public void SetItem(int i, object item) { }
1029+
}
1030+
1031+
object Item(string s, Data d)
1032+
{
1033+
return new object();
1034+
}
1035+
1036+
void Test()
1037+
{
1038+
Items items = null;
1039+
1040+
int num = 0;
1041+
1042+
for (int i = 0; i < 2; i++) {
1043+
if (num < 10)
1044+
items.SetItem(num, Item(string.Empty, new Data { Obj = null }));
1045+
}
1046+
}
1047+
}
10181048
}
10191049
}

ICSharpCode.Decompiler/IL/Transforms/LockTransform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void IBlockTransform.Run(Block block, BlockTransformContext context)
2222
// This happens in some cases:
2323
// Use correct index after transformation.
2424
if (i >= block.Instructions.Count)
25-
i = block.Instructions.Count - 1;
25+
i = block.Instructions.Count;
2626
}
2727
}
2828

ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ void IBlockTransform.Run(Block block, BlockTransformContext context)
3636
for (int i = block.Instructions.Count - 1; i >= 0; i--)
3737
{
3838
DoTransform(block, i);
39+
// This happens in some cases:
40+
// Use correct index after transformation.
41+
if (i >= block.Instructions.Count)
42+
i = block.Instructions.Count;
3943
}
4044
}
4145

0 commit comments

Comments
 (0)