From 6aa9bd8e5e1b22b25ef3a273c67107b23636dcc6 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 4 Sep 2025 22:37:03 +0200 Subject: [PATCH] Fix #3549: Do not crash on encountering nil tokens. --- ICSharpCode.Decompiler/IL/ILReader.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index 9e6236a89..072943ebd 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -212,7 +212,14 @@ namespace ICSharpCode.Decompiler.IL // Row-IDs < 1 are always invalid. throw new BadImageFormatException("Invalid metadata token"); } - return MetadataTokens.EntityHandle(token); + var handle = MetadataTokens.EntityHandle(token); + if (handle.IsNil) + { + // The runtime will crash with a BadImageFormatException when it encounters a row-ID of 0. + // We assume the code following this instruction to be unreachable. + throw new BadImageFormatException("Invalid metadata token"); + } + return handle; } IType ReadAndDecodeTypeReference()