Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a default timeout for parsing #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Foda
Copy link

@Foda Foda commented Nov 29, 2018

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);
	}
}

@azchohfi azchohfi force-pushed the master branch 10 times, most recently from 3b92274 to f3e6bd1 Compare November 17, 2021 20:36
@michael-hawker michael-hawker added this to the 8.0.0 milestone Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants