[clang] Fix a crash introduced by 3d5e9ab by adding a nullptr check. (PR #90301)

Ryosuke Niwa via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 28 23:20:11 PDT 2024


https://github.com/rniwa updated https://github.com/llvm/llvm-project/pull/90301

>From 0e9d10029e6d498d3bc5a319ac0945cf23db230d Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Fri, 26 Apr 2024 17:01:35 -0700
Subject: [PATCH 1/4] Fix a crash introduced by 3d5e9ab by adding a nullptr
 check.

---
 .../StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
index 741f336761589f..96bae0de65a5b1 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
@@ -54,7 +54,7 @@ class UncountedCallArgsChecker
       bool shouldVisitImplicitCode() const { return false; }
 
       bool TraverseDecl(Decl *D) {
-        if (isa<ClassTemplateDecl>(D) && isRefType(safeGetName(D)))
+        if (D && isa<ClassTemplateDecl>(D) && isRefType(safeGetName(D)))
           return true;
         return RecursiveASTVisitor<LocalVisitor>::TraverseDecl(D);
       }

>From 37b5f788e903ce3175e4f961389eb5a1f503c884 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Sat, 27 Apr 2024 14:03:38 -0700
Subject: [PATCH 2/4] Add a regression test.

---
 .../WebKit/call-args-regression-traverse-decl-crash.cpp    | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 clang/test/Analysis/Checkers/WebKit/call-args-regression-traverse-decl-crash.cpp

diff --git a/clang/test/Analysis/Checkers/WebKit/call-args-regression-traverse-decl-crash.cpp b/clang/test/Analysis/Checkers/WebKit/call-args-regression-traverse-decl-crash.cpp
new file mode 100644
index 00000000000000..3d8e822025f62b
--- /dev/null
+++ b/clang/test/Analysis/Checkers/WebKit/call-args-regression-traverse-decl-crash.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
+// expected-no-diagnostics
+
+template <class Class> struct T;
+template <template <class> class Class, class Type>
+struct T<Class<Type>>
+{ };

>From 9a39e456a41e415cca62866ca093708412871e00 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Sun, 28 Apr 2024 13:43:30 -0700
Subject: [PATCH 3/4] Override TraverseClassTemplateDecl instead of
 TraverseDecl.

---
 .../Checkers/WebKit/UncountedCallArgsChecker.cpp            | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
index 96bae0de65a5b1..6a5abf0c9d99db 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
@@ -53,10 +53,10 @@ class UncountedCallArgsChecker
       bool shouldVisitTemplateInstantiations() const { return true; }
       bool shouldVisitImplicitCode() const { return false; }
 
-      bool TraverseDecl(Decl *D) {
-        if (D && isa<ClassTemplateDecl>(D) && isRefType(safeGetName(D)))
+      bool TraverseClassTemplateDecl(ClassTemplateDecl *Decl) {
+        if (isRefType(safeGetName(Decl)))
           return true;
-        return RecursiveASTVisitor<LocalVisitor>::TraverseDecl(D);
+        return RecursiveASTVisitor<LocalVisitor>::TraverseClassTemplateDecl(Decl);
       }
 
       bool VisitCallExpr(const CallExpr *CE) {

>From a8cf1f9b2bf2e6f3d466108b4c4d1ba7bb5dd072 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Sun, 28 Apr 2024 23:19:45 -0700
Subject: [PATCH 4/4] Fix formatting.

---
 .../Checkers/WebKit/UncountedCallArgsChecker.cpp               | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
index 6a5abf0c9d99db..ae494de58da3da 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
@@ -56,7 +56,8 @@ class UncountedCallArgsChecker
       bool TraverseClassTemplateDecl(ClassTemplateDecl *Decl) {
         if (isRefType(safeGetName(Decl)))
           return true;
-        return RecursiveASTVisitor<LocalVisitor>::TraverseClassTemplateDecl(Decl);
+        return RecursiveASTVisitor<LocalVisitor>::TraverseClassTemplateDecl(
+            Decl);
       }
 
       bool VisitCallExpr(const CallExpr *CE) {



More information about the cfe-commits mailing list