Skip to content

Commit

Permalink
Merge pull request xoofx#710 from valterc/fix-line-group-oob
Browse files Browse the repository at this point in the history
Add line count check to avoid out of range
  • Loading branch information
xoofx authored Apr 22, 2023
2 parents 8155a1e + 512b282 commit b7cb169
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/Markdig.Tests/TestStringSliceList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,39 @@ static void Test<T>(T iterator) where T : ICharIterator
Assert.AreEqual('\0', iterator.CurrentChar); iterator.SkipChar();
}
}

[Test]
public void TestStringLineGroupCharIteratorAtCapacity()
{
string str = "ABCDEFGHI";
var text = new StringLineGroup(1)
{
// Will store the following line at capacity
new StringSlice(str, NewLine.CarriageReturnLineFeed) { Start = 0, End = 2 },
};

var iterator = text.ToCharIterator();
var chars = ToString(iterator);
TextAssert.AreEqual("ABC\r\n", chars.ToString());
TextAssert.AreEqual("ABC", text.ToString());
}

[Test]
public void TestStringLineGroupCharIteratorForcingIncreaseCapacity()
{
string str = "ABCDEFGHI";
var text = new StringLineGroup(1)
{
// Will store the following line at capacity
new StringSlice(str, NewLine.CarriageReturnLineFeed) { Start = 0, End = 2 },

// Will force increase capacity to 2 and store the line at capacity
new StringSlice(str, NewLine.CarriageReturnLineFeed) { Start = 3, End = 3 },
};

var iterator = text.ToCharIterator();
var chars = ToString(iterator);
TextAssert.AreEqual("ABC\r\nD\r\n", chars.ToString());
TextAssert.AreEqual("ABC\r\nD", text.ToString());
}
}
24 changes: 24 additions & 0 deletions src/Markdig.Tests/TestYamlFrontMatterExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,28 @@ public object Render(MarkdownObject markdownObject)
return null;
}
}

[TestCase("---\nkey1: value1\nkey2: value2\n---\n\n# Content\n")]
[TestCase("---\nkey1: value1\nkey2: value2\nkey3: value3\nkey4: value4\nkey5: value5\nkey6: value6\nkey7: value7\nkey8: value8\n---\n\n# Content\n")]
public void FrontMatterBlockLinesCharIterator(string value)
{
var builder = new MarkdownPipelineBuilder();
builder.Extensions.Add(new YamlFrontMatterExtension());
var markdownDocument = Markdown.Parse(value, builder.Build());

var yamlBlocks = markdownDocument.Descendants<YamlFrontMatterBlock>();
Assert.True(yamlBlocks.Any());

foreach (var yamlBlock in yamlBlocks)
{
var iterator = yamlBlock.Lines.ToCharIterator();
while(iterator.CurrentChar != '\0')
{
iterator.NextChar();
}
}

Assert.Pass("No exception parsing and iterating through YAML front matter block lines");
}

}
9 changes: 6 additions & 3 deletions src/Markdig/Helpers/StringLineGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,12 @@ private char NextCharNewLine()
goto Return;

MoveToNewLine:
SliceIndex++;
_offset = -1;
_currentSlice = _lines.Lines[SliceIndex];
if (SliceIndex < _lines.Lines.Length - 1)
{
SliceIndex++;
_offset = -1;
_currentSlice = _lines.Lines[SliceIndex];
}

Return:
return CurrentChar;
Expand Down

0 comments on commit b7cb169

Please sign in to comment.