Example #1
0
            public AnalysisContext(CSharpExplodedGraph explodedGraph)
            {
                check = new EmptyCollectionAccessedCheck(explodedGraph);
                check.CollectionAccessed += CollectionAccessedHandler;

                explodedGraph.AddExplodedGraphCheck(check);
            }
        private static void CheckForMultipleDispose(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var objectDisposedCheck = new ObjectDisposedPointerCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(objectDisposedCheck);

            // Store the nodes that should be reported and ignore duplicate reports for the same node.
            // This is needed because we generate two CFG blocks for the finally statements and even
            // though the syntax nodes are the same, when there is a return inside a try/catch block
            // the walked CFG paths could be different and FPs will appear.
            var nodesToReport = new Dictionary <SyntaxNode, string>();

            void memberAccessedHandler(object sender, ObjectDisposedEventArgs args)
            {
                nodesToReport[args.SyntaxNode] = args.SymbolName;
            }

            objectDisposedCheck.ObjectDisposed += memberAccessedHandler;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                objectDisposedCheck.ObjectDisposed -= memberAccessedHandler;
            }

            foreach (var item in nodesToReport)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, item.Key.GetLocation(), item.Value));
            }
        }
        private static void CheckEmptyNullableAccess(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var nullPointerCheck = new NullValueAccessedCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(nullPointerCheck);

            var nullIdentifiers = new HashSet <IdentifierNameSyntax>();

            void nullValueAccessedHandler(object sender, MemberAccessedEventArgs args) => nullIdentifiers.Add(args.Identifier);

            nullPointerCheck.ValuePropertyAccessed += nullValueAccessedHandler;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                nullPointerCheck.ValuePropertyAccessed -= nullValueAccessedHandler;
            }

            foreach (var nullIdentifier in nullIdentifiers)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, nullIdentifier.Parent.GetLocation(), nullIdentifier.Identifier.ValueText));
            }
        }
Example #4
0
        private void CheckForEmptyCollectionAccess(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var check = new EmptyCollectionAccessedCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(check);

            var emptyCollections    = new HashSet <SyntaxNode>();
            var nonEmptyCollections = new HashSet <SyntaxNode>();

            void explorationEnded(object sender, EventArgs args) => emptyCollections.Except(nonEmptyCollections)
            .Select(node => Diagnostic.Create(rule, node.GetLocation()))
            .ToList()
            .ForEach(d => context.ReportDiagnosticWhenActive(d));

            void collectionAccessedHandler(object sender, CollectionAccessedEventArgs args) =>
            (args.IsEmpty ? emptyCollections : nonEmptyCollections).Add(args.Node);

            explodedGraph.ExplorationEnded += explorationEnded;
            check.CollectionAccessed       += collectionAccessedHandler;
            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                check.CollectionAccessed       -= collectionAccessedHandler;
                explodedGraph.ExplorationEnded -= explorationEnded;
            }
        }
Example #5
0
        private static void CheckForNullDereference(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var nullPointerCheck = new NullPointerCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(nullPointerCheck);

            var nullIdentifiers = new HashSet <IdentifierNameSyntax>();

            void memberAccessedHandler(object sender, MemberAccessedEventArgs args) =>
            CollectMemberAccesses(args, nullIdentifiers, context.SemanticModel);

            nullPointerCheck.MemberAccessed += memberAccessedHandler;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                nullPointerCheck.MemberAccessed -= memberAccessedHandler;
            }

            foreach (var nullIdentifier in nullIdentifiers)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, nullIdentifier.GetLocation(), nullIdentifier.Identifier.ValueText));
            }
        }
Example #6
0
            public AnalysisContext(CSharpExplodedGraph explodedGraph)
            {
                this.objectDisposedPointerCheck = new ObjectDisposedPointerCheck(explodedGraph);
                this.objectDisposedPointerCheck.ObjectDisposed += ObjectDisposedHandler;

                explodedGraph.AddExplodedGraphCheck(this.objectDisposedPointerCheck);
            }
            public AnalysisContext(CSharpExplodedGraph explodedGraph)
            {
                nullPointerCheck = new NullValueAccessedCheck(explodedGraph);
                nullPointerCheck.ValuePropertyAccessed += AddIdentifier;

                explodedGraph.AddExplodedGraphCheck(nullPointerCheck);
            }
        private void CheckForEmptyCollectionAccess(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var check = new EmptyCollectionAccessedCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(check);

            var emptyCollections    = new HashSet <SyntaxNode>();
            var nonEmptyCollections = new HashSet <SyntaxNode>();

            EventHandler <CollectionAccessedEventArgs> collectionAccessedHandler = (sender, args) =>
                                                                                   (args.IsEmpty ? emptyCollections : nonEmptyCollections).Add(args.Node);

            check.CollectionAccessed += collectionAccessedHandler;
            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                check.CollectionAccessed -= collectionAccessedHandler;
            }

            foreach (var node in emptyCollections.Except(nonEmptyCollections))
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, node.GetLocation()));
            }
        }
Example #9
0
        private static void CheckForMultipleDispose(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var objectDisposedCheck = new ObjectDisposedPointerCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(objectDisposedCheck);

            void memberAccessedHandler(object sender, ObjectDisposedEventArgs args)
            {
                context.ReportDiagnosticWhenActive(
                    Diagnostic.Create(rule, args.Location, args.Name));
            }

            objectDisposedCheck.ObjectDisposed += memberAccessedHandler;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                objectDisposedCheck.ObjectDisposed -= memberAccessedHandler;
            }
        }
Example #10
0
 private static void CheckEmptyNullableCast(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
 {
     explodedGraph.AddExplodedGraphCheck(new NullableCastCheck(explodedGraph, context));
     explodedGraph.Walk();
 }