Browse Source

Add MetadataTokenHelpers to avoid crashing when dealing with MetadataTokens

pull/1198/head
Siegfried Pammer 7 years ago
parent
commit
d8c1796c4a
  1. 17
      ICSharpCode.Decompiler/Disassembler/MethodBodyDisassembler.cs
  2. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  3. 55
      ICSharpCode.Decompiler/Metadata/MetadataTokenHelpers.cs
  4. 2
      ILSpy/Languages/CSharpLanguage.cs

17
ICSharpCode.Decompiler/Disassembler/MethodBodyDisassembler.cs

@ -310,7 +310,7 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -310,7 +310,7 @@ namespace ICSharpCode.Decompiler.Disassembler
case OperandType.Type:
output.Write(' ');
int metadataToken = blob.ReadInt32();
EntityHandle? handle = TryAsEntityHandle(metadataToken);
EntityHandle? handle = MetadataTokenHelpers.TryAsEntityHandle(metadataToken);
try {
handle?.WriteTo(module, output, genericContext);
} catch (BadImageFormatException) {
@ -321,7 +321,7 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -321,7 +321,7 @@ namespace ICSharpCode.Decompiler.Disassembler
case OperandType.Tok:
output.Write(' ');
metadataToken = blob.ReadInt32();
handle = TryAsEntityHandle(metadataToken);
handle = MetadataTokenHelpers.TryAsEntityHandle(metadataToken);
switch (handle?.Kind) {
case HandleKind.MemberReference:
switch (metadata.GetMemberReference((MemberReferenceHandle)handle).GetKind()) {
@ -426,19 +426,6 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -426,19 +426,6 @@ namespace ICSharpCode.Decompiler.Disassembler
output.WriteLine();
}
private EntityHandle? TryAsEntityHandle(int metadataToken)
{
// SRM would interpret negative token values as virtual tokens,
// but that causes problems later on.
if (metadataToken < 0)
return null;
try {
return MetadataTokens.EntityHandle(metadataToken);
} catch (ArgumentException) {
return null;
}
}
private void WriteMetadataToken(EntityHandle handle, bool spaceBefore)
{
WriteMetadataToken(handle, MetadataTokens.GetToken(handle), spaceBefore);

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -269,6 +269,7 @@ @@ -269,6 +269,7 @@
<Compile Include="CSharp\Transforms\AddXmlDocumentationTransform.cs" />
<Compile Include="DecompileRun.cs" />
<Compile Include="Disassembler\DomExtensions.cs" />
<Compile Include="Metadata\MetadataTokenHelpers.cs" />
<Compile Include="Disassembler\OpCodeInfo.cs" />
<Compile Include="Documentation\GetPotentiallyNestedClassTypeReference.cs" />
<Compile Include="Documentation\IdStringMemberReference.cs" />

55
ICSharpCode.Decompiler/Metadata/MetadataTokenHelpers.cs

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
// Copyright (c) 2018 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Text;
namespace ICSharpCode.Decompiler.Metadata
{
public static class MetadataTokenHelpers
{
public static EntityHandle? TryAsEntityHandle(int metadataToken)
{
// SRM would interpret negative token values as virtual tokens,
// but that causes problems later on.
if (metadataToken < 0)
return null;
try {
return MetadataTokens.EntityHandle(metadataToken);
} catch (ArgumentException) {
return null;
}
}
public static EntityHandle EntityHandleOrNil(int metadataToken)
{
// SRM would interpret negative token values as virtual tokens,
// but that causes problems later on.
if (metadataToken < 0)
return MetadataTokens.EntityHandle(0);
try {
return MetadataTokens.EntityHandle(metadataToken);
} catch (ArgumentException) {
return MetadataTokens.EntityHandle(0);
}
}
}
}

2
ILSpy/Languages/CSharpLanguage.cs

@ -339,7 +339,7 @@ namespace ICSharpCode.ILSpy @@ -339,7 +339,7 @@ namespace ICSharpCode.ILSpy
output.WriteLine();
}
var corHeader = module.Reader.PEHeaders.CorHeader;
var entrypointHandle = MetadataTokens.EntityHandle(corHeader.EntryPointTokenOrRelativeVirtualAddress);
var entrypointHandle = MetadataTokenHelpers.EntityHandleOrNil(corHeader.EntryPointTokenOrRelativeVirtualAddress);
if (!entrypointHandle.IsNil && entrypointHandle.Kind == HandleKind.MethodDefinition) {
var entrypoint = metadata.GetMethodDefinition((MethodDefinitionHandle)entrypointHandle);
output.Write("// Entry point: ");

Loading…
Cancel
Save