Skip to content

Commit

Permalink
Fix potential hang when parsing LinkReferenceDefinition (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 28, 2018
1 parent cc47d33 commit c3395e6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/Markdig.Tests/Markdig.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<Content Include="hang.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="project.json" />
<None Include="Specs\GlobalizationSpecs.md" />
<None Include="Specs\JiraLinks.md" />
Expand Down
8 changes: 8 additions & 0 deletions src/Markdig.Tests/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the license.txt file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Markdig.Extensions.JiraLinks;
Expand All @@ -12,6 +13,13 @@ namespace Markdig.Tests
{
public class TestParser
{
[Test]
public void TestFixHang()
{
var input = File.ReadAllText("hang.md");
var html = Markdown.ToHtml(input);
}

[Test]
public void TestEmphasisAndHtmlEntity()
{
Expand Down
Binary file added src/Markdig.Tests/hang.md
Binary file not shown.
24 changes: 24 additions & 0 deletions src/Markdig/Helpers/StringLineGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,30 @@ public Iterator(StringLineGroup lines)

public int SliceIndex { get; private set; }

public StringLineGroup Remaining()
{
var lines = _lines;
if (CurrentChar == '\0')
{
lines.Clear();
}
else
{
for (int i = SliceIndex - 1; i >= 0; i--)
{
lines.RemoveAt(i);
}

if (lines.Lines.Length > 0)
{
lines.Lines[0].Column = _offset - lines.Lines[0].Slice.Start;
lines.Lines[0].Slice.Start = _offset;
}
}

return lines;
}

public char NextChar()
{
Start++;
Expand Down
13 changes: 1 addition & 12 deletions src/Markdig/Parsers/ParagraphBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,7 @@ private bool TryMatchLinkReferenceDefinition(ref StringLineGroup lines, BlockPro
linkReferenceDefinition.UrlSpan = linkReferenceDefinition.UrlSpan .MoveForward(startPosition);
linkReferenceDefinition.TitleSpan = linkReferenceDefinition.TitleSpan .MoveForward(startPosition);

// Remove lines that have been matched
if (iterator.Start > iterator.End)
{
lines.Clear();
}
else
{
for (int i = iterator.SliceIndex - 1; i >= 0; i--)
{
lines.RemoveAt(i);
}
}
lines = iterator.Remaining();
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/markdig.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Copyright (c) Alexandre Mutel. All rights reserved.&#xD;
This file is licensed under the BSD-Clause 2 license. &#xD;
See the license.txt file in the project root for more information.</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/NUnitProvider/SetCurrentDirectoryTo/@EntryValue">TestFolder</s:String></wpf:ResourceDictionary>

0 comments on commit c3395e6

Please sign in to comment.