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 AD0001: NRE in SE for declaration pattern #8957

Closed
pavel-mikula-sonarsource opened this issue Mar 19, 2024 · 6 comments
Closed

Fix AD0001: NRE in SE for declaration pattern #8957

pavel-mikula-sonarsource opened this issue Mar 19, 2024 · 6 comments
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: Bug Exceptions and blocking issues during analysis.

Comments

@pavel-mikula-sonarsource
Copy link
Contributor

pavel-mikula-sonarsource commented Mar 19, 2024

As reported here and observed inside ascx.cs file:

CSC : error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.SymbolicExecutionRunner' threw an exception of type 'SonarAnalyzer.SymbolicExecution.SymbolicExecutionException' with message 'Error processing method: collectiveResultRepeater_ItemDataBound
 Inner exception: System.NullReferenceException: Object reference not set to an instance of an object.
     at SonarAnalyzer.Helpers.TypeHelper.<>c__DisplayClass20_0.<Implements>b__0(INamedTypeSymbol x)
     at System.Linq.ImmutableArrayExtensions.Any[T](ImmutableArray`1 immutableArray, Func`2 predicate)
     at SonarAnalyzer.SymbolicExecution.Roslyn.OperationProcessors.IsPattern.BoolConstraintFromDeclarationPattern(ObjectConstraint valueConstraint, IDeclarationPatternOperationWrapper declaration)
     at SonarAnalyzer.SymbolicExecution.Roslyn.OperationProcessors.IsPattern.BoolConstraintFromPattern(ProgramState state, SymbolicValue value, IPatternOperationWrapper pattern)
     at SonarAnalyzer.SymbolicExecution.Roslyn.OperationProcessors.IsPattern.BoolConstraintFromOperation(ProgramState state, IIsPatternOperationWrapper operation, Boolean isLoopCondition, Int32 visitCount)
     at SonarAnalyzer.SymbolicExecution.Roslyn.OperationProcessors.BranchingProcessor`1.Process(SymbolicContext context, T operation)
     at SonarAnalyzer.SymbolicExecution.Roslyn.OperationProcessors.MultiProcessor`1.Process(SymbolicContext context)
     at SonarAnalyzer.SymbolicExecution.Roslyn.OperationDispatcher.Process(SymbolicContext context)
     at SonarAnalyzer.SymbolicExecution.Roslyn.RoslynSymbolicExecution.<ProcessOperation>d__17.MoveNext()
     at SonarAnalyzer.SymbolicExecution.Roslyn.RoslynSymbolicExecution.Execute()
     at SonarAnalyzer.Rules.SymbolicExecutionRunnerBase.AnalyzeRoslyn(SonarAnalysisContext analysisContext, SonarSyntaxNodeReportingContext nodeContext, ISymbol symbol)'. 

We don't have a reproducer (yet).

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: Bug Exceptions and blocking issues during analysis. Area: CFG/SE CFG and SE related issues. Area: C# C# rules related issues. labels Mar 19, 2024
@zsolt-kolbay-sonarsource
Copy link
Contributor

The only place where this can happen is this line, if declaration.InputType is null. Then, the null value ends up in the extension methods where an NRE is thrown.
I couldn't yet replicate it. I tried with using object, dynamic or generic types in declaration patterns.

@Tim-Pohlmann
Copy link
Contributor

The user shared the code. It looks harmless:

var collectiveEmployee = e.Item.DataItem as CollectiveBaseDto;
...
if (collectiveEmployee is CollectiveEmployeeEpldto CollectiveEPLDTOEmp)

Do you know where the call to .Any comes from in the stack trace?

@pavel-mikula-sonarsource
Copy link
Contributor Author

In case InputType is null, then InputType.DerivesOrImplements returns false , because it's null-tolerant everywhere.

@Tim-Pohlmann
Copy link
Contributor

I looked again at the stack trace: It is this line:

    private static bool Implements(this ITypeSymbol typeSymbol, ISymbol type) =>
        typeSymbol is { }
        && typeSymbol.AllInterfaces.Any(x => type.IsDefinition ? x.OriginalDefinition.Equals(type) : x.Equals(type));

It can throw an NRE if type, x or x.OriginalDefinition are null.

  • type is null if declaration.NarrowedType is null.
  • x is null if typeSymbol.AllInterfaces contains a null reference (probably not possible?).
  • Can x.OriginalDefinition be null?

The first one seems the most likely to me, but I'm not sure.

@pavel-mikula-sonarsource
Copy link
Contributor Author

This makes sense.

  • type is null if declaration.NarrowedType is null.

AllInterfaces returns ImmutableArray that is a struct and cannot be null
OriginalDefinition cannot be null, it defaults to this.

@mary-georgiou-sonarsource
Copy link
Contributor

I tried with VSBuild Tools 2019 and 2022.
I could not reproduce it.

@mary-georgiou-sonarsource mary-georgiou-sonarsource closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: Bug Exceptions and blocking issues during analysis.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants