From 6b075326f04b58d61096839a6e25b1148a157045 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 5 Aug 2019 23:09:02 +0200 Subject: [PATCH] Fix #1615: Handle nil tokens gracefully. --- ICSharpCode.Decompiler/IL/ILReader.cs | 3 ++- ICSharpCode.Decompiler/IL/InstructionOutputExtensions.cs | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index 88b48da73..730198abf 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -124,9 +124,10 @@ namespace ICSharpCode.Decompiler.IL EntityHandle ReadAndDecodeMetadataToken() { int token = reader.ReadInt32(); - if (token < 0) { + if (token <= 0) { // SRM uses negative tokens as "virtual tokens" and can get confused // if we manually create them. + // Row-IDs < 1 are always invalid. throw new BadImageFormatException("Invalid metadata token"); } return MetadataTokens.EntityHandle(token); diff --git a/ICSharpCode.Decompiler/IL/InstructionOutputExtensions.cs b/ICSharpCode.Decompiler/IL/InstructionOutputExtensions.cs index d1aab3e59..b246ac5b9 100644 --- a/ICSharpCode.Decompiler/IL/InstructionOutputExtensions.cs +++ b/ICSharpCode.Decompiler/IL/InstructionOutputExtensions.cs @@ -69,8 +69,10 @@ namespace ICSharpCode.Decompiler.IL public static void WriteTo(this EntityHandle entity, PEFile module, ITextOutput output, Metadata.GenericContext genericContext, ILNameSyntax syntax = ILNameSyntax.Signature) { - if (entity.IsNil) - throw new ArgumentNullException(nameof(entity)); + if (entity.IsNil) { + output.Write(""); + return; + } if (module == null) throw new ArgumentNullException(nameof(module)); var metadata = module.Metadata;