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 Symbolic Execution: Invalid nameof() should be properly handled #423

Closed
tamasvajk opened this issue Jun 12, 2017 · 2 comments
Closed
Assignees
Labels
Type: Bug Exceptions and blocking issues during analysis.
Milestone

Comments

@tamasvajk
Copy link
Contributor

Description

The following invalid code results in AD0001 diagnostics:

static void Main()
{
    var x = $"{nameof()}";
}

Stack trace:

Severity	Code	Description	Project	File	Line	Suppression State	Detail Description
Warning	AD0001	Analyzer 'SonarAnalyzer.Rules.CSharp.NullPointerDereference' threw an exception of type 'System.InvalidOperationException' with message 'This operation does not apply to an empty instance.'.	ConsoleApp1		1	Active	Analyzer 'SonarAnalyzer.Rules.CSharp.NullPointerDereference' threw the following exception:
'Exception occurred with following context:
Compilation: ConsoleApp1
SyntaxTree: ...
SyntaxNode: static void Main() ... [MethodDeclarationSyntax]@[183..272) (10,8)-(13,9)

System.InvalidOperationException: This operation does not apply to an empty instance.
   at System.Collections.Immutable.ImmutableStack`1.Peek()
   at SonarAnalyzer.Helpers.FlowAnalysis.Common.ProgramState.PopValue(SymbolicValue& poppedValue)
   at SonarAnalyzer.Helpers.FlowAnalysis.Common.InvocationVisitor.HandleNameofExpression()
   at SonarAnalyzer.Helpers.FlowAnalysis.Common.InvocationVisitor.ProcessInvocation()
   at SonarAnalyzer.Helpers.FlowAnalysis.CSharp.ExplodedGraph.VisitInstruction(ExplodedGraphNode node)
   at SonarAnalyzer.Helpers.FlowAnalysis.Common.ExplodedGraph.Walk()
   at SonarAnalyzer.Rules.CSharp.NullPointerDereference.CheckForNullDereference(ExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
   at SonarAnalyzer.Rules.CSharp.NullPointerDereference.<>c.<Initialize>b__5_0(ExplodedGraph e, SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.FlowAnalysis.CSharp.FlowAnalysisExtensions.Analyze(CSharpSyntaxNode declarationBody, ISymbol symbol, Action`2 analyze, SyntaxNodeAnalysisContext context)
   at SonarAnalyzer.Helpers.FlowAnalysis.CSharp.FlowAnalysisExtensions.<>c__DisplayClass0_0.<RegisterExplodedGraphBasedAnalysis>b__1(SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.DiagnosticAnalyzerContextHelper.<>c__DisplayClass0_0`1.<RegisterSyntaxNodeActionInNonGenerated>b__0(SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.SonarAnalysisContext.<>c__DisplayClass12_0`1.<RegisterSyntaxNodeAction>b__0(SyntaxNodeAnalysisContext c)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass42_1`1.<ExecuteSyntaxNodeAction>b__1()
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable`1 info)
-----
'.
@Evangelink Evangelink added Area: Rules Type: Bug Exceptions and blocking issues during analysis. labels Jun 12, 2017
@Evangelink Evangelink modified the milestones: 6.1, 6.0 Jun 12, 2017
@Evangelink Evangelink self-assigned this Jun 14, 2017
@Evangelink Evangelink changed the title Invalid nameof() call causes AD0001 Fix Symbolic Execution: Invalid nameof() should be properly handled Jun 14, 2017
@gharika
Copy link

gharika commented Jul 5, 2017

This is also happening for valid calls

throw new ArgumentNullException(nameof(collection));

@valhristov
Copy link
Contributor

Hi @gharika,

Could you please send us a minimal project that can be used to reproduce the problem? I wasn't able to reproduce it with just the provided snipped and I tried with several variants... You can send it directly to my email: valeri dot hristov at sonarsource dot com

Thanks in advance,
Valeri

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Exceptions and blocking issues during analysis.
Projects
None yet
Development

No branches or pull requests

4 participants