From 655d831df763a8366b1c314c901f3b291c2d7b38 Mon Sep 17 00:00:00 2001 From: Kirill Osenkov Date: Fri, 29 Oct 2021 15:06:59 -0700 Subject: [PATCH] Protect against circular type forwarders If we have two assemblies with type forwarders that point to each other, we enter an infinite loop and a stack overflow. This breaks the cycle by detecting reentrancy. Fixes https://github.com/jbevain/cecil/issues/706 --- Mono.Cecil/ExportedType.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Mono.Cecil/ExportedType.cs b/Mono.Cecil/ExportedType.cs index 8238a0c6d..8b7d00a27 100644 --- a/Mono.Cecil/ExportedType.cs +++ b/Mono.Cecil/ExportedType.cs @@ -225,9 +225,21 @@ public override string ToString () return FullName; } + private bool reentrancyGuard = false; + public TypeDefinition Resolve () { - return module.Resolve (CreateReference ()); + if (reentrancyGuard) { + throw new InvalidOperationException ($"Circularity when resolving exported type {this}"); + } + + reentrancyGuard = true; + try { + return module.Resolve (CreateReference ()); + } + finally { + reentrancyGuard = false; + } } internal TypeReference CreateReference ()