From bd4ea56d2a86f8891d85646e65ab64d70b61d07a Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Tue, 5 Dec 2017 09:34:59 +0100 Subject: [PATCH] Fix potential StackOverflow exception when processing deep nested `|` delimiters (#179) --- src/Markdig/Syntax/Inlines/Inline.cs | 35 ++++++++++++---------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/Markdig/Syntax/Inlines/Inline.cs b/src/Markdig/Syntax/Inlines/Inline.cs index 4d405a7f..43311fdc 100644 --- a/src/Markdig/Syntax/Inlines/Inline.cs +++ b/src/Markdig/Syntax/Inlines/Inline.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// Copyright (c) Alexandre Mutel. All rights reserved. // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using System; @@ -192,17 +192,16 @@ namespace Markdig.Syntax.Inlines /// true if this instance contains a parent of the specified type; false otherwise public bool ContainsParentOfType() where T : Inline { - var delimiter = this as T; - if (delimiter != null) + var inline = this; + while (inline != null) { - return true; + var delimiter = inline as T; + if (delimiter != null) + { + return true; + } + inline = inline.Parent; } - - if (Parent != null) - { - return Parent.ContainsParentOfType(); - } - return false; } @@ -213,19 +212,15 @@ namespace Markdig.Syntax.Inlines /// An enumeration on the parents of the specified type public IEnumerable FindParentOfType() where T : Inline { - var parent = Parent; - var delimiter = this as T; - if (delimiter != null) + var inline = this; + while (inline != null) { - yield return delimiter; - } - - if (parent != null) - { - foreach (var previous in parent.FindParentOfType()) + var delimiter = inline as T; + if (delimiter != null) { - yield return previous; + yield return delimiter; } + inline = inline.Parent; } }