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

Fix a NullReferenceException in DocumentExtensions #10819

Closed
wants to merge 2 commits into from

Conversation

aelij
Copy link
Contributor

@aelij aelij commented Apr 24, 2016

I encountered this while using the Classifier. This happens if you specify a span outside the the document. I wasn't sure if it should return the entire document's semantic model or throw an ArgumentOutOfRangeException; I chose the former.

Stack trace:

at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxFactsService.GetMemberBodySpanForSpeculativeBinding(SyntaxNode node)
at async Microsoft.CodeAnalysis.Shared.Extensions.DocumentExtensions.GetSemanticModelForNodeAsync(ISemanticModelService semanticModelService, ISyntaxFactsService syntaxFactService, Document document, SyntaxNode node, TextSpan span, CancellationToken cancellationToken)
at async Microsoft.CodeAnalysis.Shared.Extensions.DocumentExtensions.GetSemanticModelForSpanAsync(?)
at async Microsoft.CodeAnalysis.Classification.Classifier.GetClassifiedSpansAsync(?)

@Pilchie
Copy link
Member

Pilchie commented Apr 24, 2016

Hi @aelij,

Thanks for the submission! However, I think I personally would prefer to have this throw. It's generally a programming error to ask for a span outside the span of the document. Did you run into this with a regular file in Visual Studio, or did was it when you were using the API?

Anyone else on @dotnet/roslyn-ide want to comment? @mattwar?

if (node == null)
{
return await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style Nit: We generally prefer a blank line after a }.

@jasonmalinowski
Copy link
Member

Agreed -- this should throw in that case. (I wonder what bugs we'll uncover with that!)

@aelij
Copy link
Contributor Author

aelij commented Apr 26, 2016

Is throw new ArgumentOutOfRangeException(nameof(span)) enough or should I add some message?

@Pilchie I ran into this while using the API to hook up syntax highlighting in AvalonEdit.

@@ -59,6 +59,10 @@ public static bool IsOpen(this Document document)
}

var node = token.Parent.AncestorsAndSelf().FirstOrDefault(a => a.FullSpan.Contains(span));
if (node == null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrmmm... how could node be null? When would we have a span that wasn't contained in the full span of some node in the document?

@CyrusNajmabadi
Copy link
Member

I would "throw an ArgumentOutOfRangeException". The span is not within bounds. There is something very wrong with the caller.

@aelij
Copy link
Contributor Author

aelij commented May 8, 2016

In case it got missed - I pushed the requested changes to my branch.

@davkean davkean added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label May 8, 2016
@davkean
Copy link
Member

davkean commented May 8, 2016

@dotnet/roslyn-ide Can you take another look at it?

@@ -59,6 +59,11 @@ public static bool IsOpen(this Document document)
}

var node = token.Parent.AncestorsAndSelf().FirstOrDefault(a => a.FullSpan.Contains(span));
if (node == null)
{
throw new ArgumentOutOfRangeException(nameof(span));
Copy link
Member

@jasonmalinowski jasonmalinowski May 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems more reasonable, should we just do if (!root.FullSpan.Contains(span)) earlier and throw instead? This seems to be a fairly circuitous way to check the simpler case.

@DustinCampbell
Copy link
Member

@Pilchie, @CyrusNajmabadi, @jasonmalinowski: What's the status on this PR?

@jasonmalinowski
Copy link
Member

I guess we are waiting for @aelij to respond to my suggestion?

@aelij
Copy link
Contributor Author

aelij commented Dec 1, 2016

Oh, I was waiting for something actionable... :)

I'd leave it as is, since obviously this isn't a common code path, so it's a bit of a waste to check the span's range on every call.

@jaredpar
Copy link
Member

We apologize, but we are closing this PR due to code drift. We regret letting this PR go so long without attention, but at this point the code base has changed too much for us to revisit older PRs. Going forward, we will manage PRs better under an SLA currently under draft in issue #26266 – we encourage you to add comments to that draft as you see fit. Although that SLA is still in draft form, we nevertheless want to move forward with the identification of older PRs to give contributors as much early notice as possible to review and update them.

If you are interested in pursuing this PR, please reset it against the head of master and we will reopen it. Thank you!

@jaredpar jaredpar closed this Apr 19, 2018
@jaredpar jaredpar added the Resolution-Expired The request is closed due to inactivity under our contribution review policy. label Apr 19, 2018
@aelij aelij deleted the fix-classifier-nullref branch November 4, 2022 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE cla-already-signed Community The pull request was submitted by a contributor who is not a Microsoft employee. Resolution-Expired The request is closed due to inactivity under our contribution review policy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants