Browse Source

Fix stack overflow in RequiredNamespaceCollector.

pull/1030/head
Daniel Grunwald 7 years ago
parent
commit
2d04f24670
  1. 30
      ICSharpCode.Decompiler/CSharp/RequiredNamespaceCollector.cs
  2. 2
      ICSharpCode.Decompiler/IL/ILReader.cs

30
ICSharpCode.Decompiler/CSharp/RequiredNamespaceCollector.cs

@ -37,7 +37,7 @@ namespace ICSharpCode.Decompiler.CSharp
static readonly Decompiler.TypeSystem.GenericContext genericContext = default; static readonly Decompiler.TypeSystem.GenericContext genericContext = default;
public static void CollectNamespaces(IEntity entity, MetadataModule module, public static void CollectNamespaces(IEntity entity, MetadataModule module,
HashSet<string> namespaces, CodeMappingInfo mappingInfo = null, bool scanningFullType = false) HashSet<string> namespaces, CodeMappingInfo mappingInfo = null)
{ {
if (entity == null || entity.MetadataToken.IsNil) if (entity == null || entity.MetadataToken.IsNil)
return; return;
@ -57,23 +57,23 @@ namespace ICSharpCode.Decompiler.CSharp
} }
foreach (var nestedType in td.NestedTypes) { foreach (var nestedType in td.NestedTypes) {
CollectNamespaces(nestedType, module, namespaces, mappingInfo, scanningFullType: true); CollectNamespaces(nestedType, module, namespaces, mappingInfo);
} }
foreach (var field in td.Fields) { foreach (var field in td.Fields) {
CollectNamespaces(field, module, namespaces, mappingInfo, scanningFullType: true); CollectNamespaces(field, module, namespaces, mappingInfo);
} }
foreach (var property in td.Properties) { foreach (var property in td.Properties) {
CollectNamespaces(property, module, namespaces, mappingInfo, scanningFullType: true); CollectNamespaces(property, module, namespaces, mappingInfo);
} }
foreach (var @event in td.Events) { foreach (var @event in td.Events) {
CollectNamespaces(@event, module, namespaces, mappingInfo, scanningFullType: true); CollectNamespaces(@event, module, namespaces, mappingInfo);
} }
foreach (var method in td.Methods) { foreach (var method in td.Methods) {
CollectNamespaces(method, module, namespaces, mappingInfo, scanningFullType: true); CollectNamespaces(method, module, namespaces, mappingInfo);
} }
break; break;
case IField field: case IField field:
@ -99,7 +99,7 @@ namespace ICSharpCode.Decompiler.CSharp
foreach (var part in parts) { foreach (var part in parts) {
var methodDef = module.metadata.GetMethodDefinition(part); var methodDef = module.metadata.GetMethodDefinition(part);
var body = reader.GetMethodBody(methodDef.RelativeVirtualAddress); var body = reader.GetMethodBody(methodDef.RelativeVirtualAddress);
CollectNamespacesFromMethodBody(body, module, namespaces, scanningFullType: scanningFullType); CollectNamespacesFromMethodBody(body, module, namespaces);
} }
} }
break; break;
@ -170,7 +170,7 @@ namespace ICSharpCode.Decompiler.CSharp
} }
} }
static void CollectNamespacesFromMethodBody(MethodBodyBlock method, MetadataModule module, HashSet<string> namespaces, bool scanningFullType = false) static void CollectNamespacesFromMethodBody(MethodBodyBlock method, MetadataModule module, HashSet<string> namespaces)
{ {
var metadata = module.metadata; var metadata = module.metadata;
var instructions = method.GetILReader(); var instructions = method.GetILReader();
@ -207,7 +207,7 @@ namespace ICSharpCode.Decompiler.CSharp
case HandleKind.MethodSpecification: case HandleKind.MethodSpecification:
case HandleKind.MemberReference: case HandleKind.MemberReference:
CollectNamespacesForMemberReference(module.ResolveEntity(handle, genericContext) as IMember, CollectNamespacesForMemberReference(module.ResolveEntity(handle, genericContext) as IMember,
module, namespaces, scanningFullType: scanningFullType); module, namespaces);
break; break;
case HandleKind.StandaloneSignature: case HandleKind.StandaloneSignature:
var sig = metadata.GetStandaloneSignature((StandaloneSignatureHandle)handle); var sig = metadata.GetStandaloneSignature((StandaloneSignatureHandle)handle);
@ -228,21 +228,15 @@ namespace ICSharpCode.Decompiler.CSharp
} }
} }
static void CollectNamespacesForMemberReference(IMember member, MetadataModule module, HashSet<string> namespaces, bool scanningFullType = false) static void CollectNamespacesForMemberReference(IMember member, MetadataModule module, HashSet<string> namespaces)
{ {
switch (member) { switch (member) {
case IField field: case IField field:
if (!scanningFullType && field.IsCompilerGeneratedOrIsInCompilerGeneratedClass()) CollectNamespacesForTypeReference(field.DeclaringType, namespaces);
CollectNamespaces(field, module, namespaces);
else
CollectNamespacesForTypeReference(field.DeclaringType, namespaces);
CollectNamespacesForTypeReference(field.ReturnType, namespaces); CollectNamespacesForTypeReference(field.ReturnType, namespaces);
break; break;
case IMethod method: case IMethod method:
if (!scanningFullType && method.IsCompilerGeneratedOrIsInCompilerGeneratedClass()) CollectNamespacesForTypeReference(method.DeclaringType, namespaces);
CollectNamespaces(method, module, namespaces);
else
CollectNamespacesForTypeReference(method.DeclaringType, namespaces);
CollectNamespacesForTypeReference(method.ReturnType, namespaces); CollectNamespacesForTypeReference(method.ReturnType, namespaces);
foreach (var param in method.Parameters) foreach (var param in method.Parameters)
CollectNamespacesForTypeReference(param.Type, namespaces); CollectNamespacesForTypeReference(param.Type, namespaces);

2
ICSharpCode.Decompiler/IL/ILReader.cs

@ -989,7 +989,7 @@ namespace ICSharpCode.Decompiler.IL
case ILOpCode.Unbox_any: case ILOpCode.Unbox_any:
return Push(new UnboxAny(Pop(), ReadAndDecodeTypeReference())); return Push(new UnboxAny(Pop(), ReadAndDecodeTypeReference()));
default: default:
return new InvalidBranch("Unknown opcode: " + opCode.ToString()); return new InvalidBranch($"Unknown opcode: 0x{(int)opCode:X2}");
} }
} }

Loading…
Cancel
Save