-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Fix StackOverflowException in RoutePatternAnalyzer #48105
Conversation
cancellationToken.ThrowIfCancellationRequested(); | ||
var current = stack.Pop(); | ||
|
||
foreach (var child in current.ChildNodesAndTokens()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code I added follows what the Regex analyzer does. I investigated why the regex analyzer doesn't use DecendantTokens
. I think it could have, but the recently added FilteredSpans
check wouldn't work with the helper method:
It looks like FilteredSpans
has only just been made public. aspnetcore probably won't update to bits with it until after .NET 8. I'd like to leave the method as is so using FilteredSpans in the future is possible.
Btw, what are filtered spans? I'm guessing it's an optimization, but I'd be interested if you could point me at more detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding @mavasani for the question on filtered spans.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FilterSpan is the new API added on analysis context types when computing diagnostics for lightbulb. This allows expensive analyzers to optimize their performance based on the span within the tree for which diagnostics are being computed. If the analyzer ignores this span, the analyzer driver will filter the reported diagnostics outside this span, there should be no functional impact of ignoring this filter span.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This analyzer is expensive. Do you recommend eventually using filter span like other analyzers are doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely - all SemanticModelAnalysisContext callbacks should respect the filter span. I plan to write a meta-analyzer in Microsoft.CodeAnalysis.Analyzers package to recommend the same. These analyzers are known to be the biggest beneficiaries of this API on lightbulb path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand why DescendantTokens
is in compatible with FilteredSpans
. It can be implemented by passing down the predicate correct? Having every analyzer / generator out there build their own stack for navigating the tree to scan tokens is not the right solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see there is a DescendantTokens
overload that takes a span: https://github.com/dotnet/roslyn/blob/252def9efa028af3f0562929b4b0e8a74dcd3124/src/Compilers/Core/Portable/Syntax/SyntaxNode.cs#L1049
It looks like the overload checks span overlaps.
I think the below would work (assuming FilterSpan was available):
foreach (var item in root.DescendantTokens(context.FilterSpan))
{
cancellationToken.ThrowIfCancellationRequested();
AnalyzeToken(context, routeUsageCache, item, cancellationToken);
}
DescendantTokens
allocates an enumerator, but this method was allocating a collection before anyway. I'll switch the PR to use it and we can add FilterSpan
when it's available.
src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternAnalyzer.cs
Show resolved
Hide resolved
…anguage/RoutePatternAnalyzer.cs
Fixes #48097
Change semantic model analysis to use a
Stack<T>
instead of recursion. This removes the possibility of running out of stack and causing a stack overflow in files with deeply nested node graphs.