Knagis/CommonMark.NET

System.IndexOutOfRangeException parsing markdown with invalid HTML

Closed this issue · 12 comments

Hello,

I originally logged this an issue with Markdown Edit https://github.com/mike-ward/Markdown-Edit/issues/159, but the crash is with CommonMark.NET. The following code snippet quite handily reproduces the crash using the latest version of CommonMark.NET

string source;
string output;
CommonMarkSettings settings;

settings = CommonMarkSettings.Default.Clone();
settings.AdditionalFeatures = CommonMarkAdditionalFeatures.All;

source = "Title\r\n=====\r\n\r\nSome HTML with\r\na line break<";

output = CommonMarkConverter.Convert(source, settings);

Console.WriteLine(output);

Basically, that floating < character at the end of the source is causing the crash. I experienced it when pasting HTML into a Markdown Edit window then deleting the tags as I converted it from HTML to markdown manually.

CommonMark.CommonMarkException: An error occurred during inline parsing. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at CommonMark.Parser.Scanner.scan_autolink_uri(String s, Int32 pos, Int32 sourceLength) in E:\OneDrive\Programming\CommonMark\CommonMark\Parser\Scanner.cs:line 15
   at CommonMark.Parser.InlineMethods.handle_pointy_brace(Subject subj) in E:\OneDrive\Programming\CommonMark\CommonMark\Parser\InlineMethods.cs:line 851
   at CommonMark.Parser.InlineMethods.ParseInline(Subject subj, Func`2[] parsers, Char[] specialCharacters) in E:\OneDrive\Programming\CommonMark\CommonMark\Parser\InlineMethods.cs:line 989
   at CommonMark.Parser.InlineMethods.parse_inlines(Subject subj, Func`2[] parsers, Char[] specialCharacters) in E:\OneDrive\Programming\CommonMark\CommonMark\Parser\InlineMethods.cs:line 1023
   at CommonMark.Parser.BlockMethods.ProcessInlines(Block block, DocumentData data, CommonMarkSettings settings) in E:\OneDrive\Programming\CommonMark\CommonMark\Parser\BlockMethods.cs:line 300
   at CommonMark.CommonMarkConverter.ProcessStage2(Block document, CommonMarkSettings settings) in E:\OneDrive\Programming\CommonMark\CommonMark\CommonMarkConverter.cs:line 168
   --- End of inner exception stack trace ---
   at CommonMark.CommonMarkConverter.ProcessStage2(Block document, CommonMarkSettings settings) in E:\OneDrive\Programming\CommonMark\CommonMark\CommonMarkConverter.cs:line 181
   at CommonMark.CommonMarkConverter.Convert(TextReader source, TextWriter target, CommonMarkSettings settings) in E:\OneDrive\Programming\CommonMark\CommonMark\CommonMarkConverter.cs:line 294
   at CommonMark.CommonMarkConverter.Convert(String source, CommonMarkSettings settings) in E:\OneDrive\Programming\CommonMark\CommonMark\CommonMarkConverter.cs:line 314
   at ScratchPad.Program.Main(String[] args) in C:\Checkout\cyotek\source\Misc\ScratchPad\Program.cs:line 101

Regards;
Richard Moss

I will fix this as soon as possible, hopefully tomorrow.

v0.13.0 has been released to nuget with the fix

Thank you!

I updated my test project from v0.12 to v0.13 but I get exactly the same crash. The DLL has today's timestamp so would appear to be the correct version (hard to tell when all the version info is 0.1 :)) but unfortunately the issue doesn't seem resolved.

Pre-submit-edit: I checked the decompiled DLL and can confirm that it's missing the commented out line you added from the commit, but it's still very much broken for me.

Ok, how did I manage to do that... That line should not have been commented out - I commented it out to check that the test actually fails without it and then did not put it back...

I will push a new release this evening, this time with more coffee preceding it...

Everyone is human :)

1.13.1 released. Hopefully I didn't mess up again :)

I hate to be the bearer of bad news... but I'm afraid something still isn't right. I grabbed the latest package and ran my test program again, and it still crashed.

I decompiled one of the 13.1 DLL's again and compared it with the commit, and it's missing the if (pos >= sourceLength) return 0; line still. As I can see that line is correct in the source code my best guess is the updated source wasn't compiled before building the package. The files do have today's timestamp and match the timestamp of your commit, but ... well, it's broken.

Just in case something had gone wrong, I did do a forced reinstall of the package to no avail.

As a virtual messenger I'm glad I can't be shot!

I suspect I know where I didn't do proper rebuild.

Double coffee didn't help, I guess third time's the charm...

OK, 1.13.2 has been pushed to nuget. I do hope the streak of bad luck ends here...

I can confirm the streak of bad luck ends, just upgraded the test project to 13.2 and it works flawlessly. Thanks very much!

@Knagis you should've skipped right to 0.14 :)