Skip to content

Commit 51a2618

Browse files
Fix #3492: Do not crash, if field used by RuntimeHelpers.InitializeArray is malformed.
1 parent 3fd3071 commit 51a2618

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

ICSharpCode.Decompiler/IL/Transforms/TransformArrayInitializers.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,23 @@ bool MatchInitializeArrayCall(ILInstruction instruction, out ILInstruction array
843843

844844
bool HandleRuntimeHelpersInitializeArray(Block body, int pos, ILVariable array, IType arrayType, int[] arrayLength, out ILInstruction[] values, out int foundPos)
845845
{
846+
values = null;
847+
foundPos = -1;
846848
if (MatchInitializeArrayCall(body.Instructions[pos], out var arrayInst, out var field) && arrayInst.MatchLdLoc(array))
847849
{
848850
if (field.HasFlag(System.Reflection.FieldAttributes.HasFieldRVA))
849851
{
850852
var valuesList = new List<ILInstruction>();
851-
var initialValue = field.GetInitialValue(context.PEFile, context.TypeSystem);
853+
BlobReader initialValue;
854+
try
855+
{
856+
initialValue = field.GetInitialValue(context.PEFile, context.TypeSystem);
857+
}
858+
catch (BadImageFormatException ex)
859+
{
860+
array.Function.Warnings.Add($"IL_{body.Instructions[pos].ILRanges.FirstOrDefault().Start:x4}: {ex.Message}");
861+
return false;
862+
}
852863
if (DecodeArrayInitializer(arrayType, initialValue, arrayLength, valuesList))
853864
{
854865
values = valuesList.ToArray();
@@ -857,8 +868,6 @@ bool HandleRuntimeHelpersInitializeArray(Block body, int pos, ILVariable array,
857868
}
858869
}
859870
}
860-
values = null;
861-
foundPos = -1;
862871
return false;
863872
}
864873

0 commit comments

Comments
 (0)