ColorCode-Universal icon indicating copy to clipboard operation
ColorCode-Universal copied to clipboard

Added a default timeout for parsing

Open Foda opened this issue 6 years ago • 0 comments
trafficstars

I've encountered an issue specifically with the CSS parsing regex where a line that doesn't end in a } and is quite a long will cause an extremely long parsing time (more than a few mins).

To fix this issue, I've added a defaultParseTimeoutSec parameter to the CodeColorizerBase class that allows you to specify a default parse timeout. This allows a RegexMatchTimeoutException to be thrown when calling LanguageParser.Parse

Here's a minimal example of the issue:

Dotnetfiddle version (should take ~6 seconds to run, but adding more items will cause the time to go up very quickly)

Code:

using System;
using System.Text.RegularExpressions;
					
public class Program
{
	public static void Main()
	{
		Regex regex = new Regex(@"(?msi)(?:(\s*/\*.*?\*/)|(([a-z0-9#. \[\]=\"":_-]+)\s*(?:,\s*|{))+(?:(\s*/\*.*?\*/)|(?:\s*([a-z0-9 -]+\s*):\s*([a-z0-9#,<>\?%. \(\)\\\/\*\{\}:'\""!_=-]+);?))*\s*})");
		var regexMatch = regex.Match("   a, abbr, acronym, address, applet, article, aside, audio, b, big, blockquote, body, canvas, caption, center, cite, code {");
		Console.WriteLine(regexMatch.Captures.Count);
	}
}

Foda avatar Nov 29 '18 19:11 Foda