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

Avoid compiler hang from invalid text in AdditionalFiles #868

Merged
merged 4 commits into from Aug 18, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/Microsoft.VisualStudio.Threading.Analyzers/CommonInterest.cs
Expand Up @@ -63,9 +63,11 @@ internal static class CommonInterest

private const RegexOptions FileNamePatternRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Singleline;

private static readonly Regex NegatableTypeOrMemberReferenceRegex = new Regex(@"^(?<negated>!)?\[(?<typeName>[^\[\]\:]+)+\](?:\:\:(?<memberName>\S+))?\s*$", RegexOptions.Singleline | RegexOptions.CultureInvariant);
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(5); // Prevent expensive CPU hang in Regex.Match if backtracking occurs due to pathological input (see #485).

private static readonly Regex MemberReferenceRegex = new Regex(@"^\[(?<typeName>[^\[\]\:]+)+\]::(?<memberName>\S+)\s*$", RegexOptions.Singleline | RegexOptions.CultureInvariant);
private static readonly Regex NegatableTypeOrMemberReferenceRegex = new Regex(@"^(?<negated>!)?\[(?<typeName>[^\[\]\:]+)+\](?:\:\:(?<memberName>\S+))?\s*$", RegexOptions.Singleline | RegexOptions.CultureInvariant, RegexMatchTimeout);

private static readonly Regex MemberReferenceRegex = new Regex(@"^\[(?<typeName>[^\[\]\:]+)+\]::(?<memberName>\S+)\s*$", RegexOptions.Singleline | RegexOptions.CultureInvariant, RegexMatchTimeout);

/// <summary>
/// An array with '.' as its only element.
Expand All @@ -84,8 +86,17 @@ internal static IEnumerable<TypeMatchSpec> ReadTypesAndMembers(AnalyzerOptions a
{
foreach (string line in ReadAdditionalFiles(analyzerOptions, fileNamePattern, cancellationToken))
{
Match match = NegatableTypeOrMemberReferenceRegex.Match(line);
if (!match.Success)
Match? match = null;
try
{
match = NegatableTypeOrMemberReferenceRegex.Match(line);
}
catch (RegexMatchTimeoutException)
{
throw new InvalidOperationException($"Regex.Match timeout when parsing line: {line}");
}

if (match == null || !match.Success)
{
throw new InvalidOperationException($"Parsing error on line: {line}");
bluetarpmedia marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -175,8 +186,17 @@ internal static IEnumerable<string> ReadLinesFromAdditionalFile(SourceText text)

internal static QualifiedMember ParseAdditionalFileMethodLine(string line)
{
Match match = MemberReferenceRegex.Match(line);
if (!match.Success)
Match? match = null;
try
{
match = MemberReferenceRegex.Match(line);
}
catch (RegexMatchTimeoutException)
{
throw new InvalidOperationException($"Regex.Match timeout when parsing line: {line}");
}

if (match == null || !match.Success)
{
throw new InvalidOperationException($"Parsing error on line: {line}");
}
Expand Down