[clang] [clang][StaticAnalyzer] Crash on loop unrolling mode (PR #82089)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 19 19:33:49 PST 2024


https://github.com/huang-me updated https://github.com/llvm/llvm-project/pull/82089

>From 2802ef4b9ed88da3cac3333b16ab7738907ee806 Mon Sep 17 00:00:00 2001
From: huang-me <amos0107 at gmail.com>
Date: Sat, 17 Feb 2024 10:43:48 +0800
Subject: [PATCH 1/3] Fix crash on StaticAnalyzer loop unrolling

---
 clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index a80352816be613..4001268bde6677 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -226,6 +226,17 @@ static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
           return false;
       }
     }
+
+    if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(S)) {
+      for (const Stmt *CB : dyn_cast<CompoundStmt>(SS->getBody())->body()) {
+        for (const Decl *D : dyn_cast<DeclStmt>(CB)->decls()) {
+          // Once we reach the declaration of the VD we can return.
+          if (D->getCanonicalDecl() == VD)
+            return false;
+        }
+      }
+    }
+
     // Check the usage of the pass-by-ref function calls and adress-of operator
     // on VD and reference initialized by VD.
     ASTContext &ASTCtx =

>From e9e195e4462da7f3ca2317096ddace6ce3e88d13 Mon Sep 17 00:00:00 2001
From: huang-me <amos0107 at gmail.com>
Date: Mon, 19 Feb 2024 18:17:27 +0800
Subject: [PATCH 2/3] Check if dynamic cast get pointer to valid elements

---
 clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index 4001268bde6677..093e9bbf4ce5e0 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -228,11 +228,15 @@ static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
     }
 
     if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(S)) {
-      for (const Stmt *CB : dyn_cast<CompoundStmt>(SS->getBody())->body()) {
-        for (const Decl *D : dyn_cast<DeclStmt>(CB)->decls()) {
-          // Once we reach the declaration of the VD we can return.
-          if (D->getCanonicalDecl() == VD)
-            return false;
+      if (const CompoundStmt *CST = dyn_cast<CompoundStmt>(SS->getBody())) {
+        for (const Stmt *CB : CST->body()) {
+          if (const DeclStmt *DST = dyn_cast<DeclStmt>(CB)) {
+            for (const Decl *D : DST->decls()) {
+              // Once we reach the declaration of the VD we can return.
+              if (D->getCanonicalDecl() == VD)
+                return false;
+            }
+          }
         }
       }
     }

>From 6ed9ea88865e91f1727077b1a3a24d7b110060fd Mon Sep 17 00:00:00 2001
From: huang-me <amos0107 at gmail.com>
Date: Tue, 20 Feb 2024 11:31:23 +0800
Subject: [PATCH 3/3] Add testcase for finding declaration within SwitchStmt

---
 .../test-escaping-on-var-before-switch-case.c         | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 clang/test/Analysis/test-escaping-on-var-before-switch-case.c

diff --git a/clang/test/Analysis/test-escaping-on-var-before-switch-case.c b/clang/test/Analysis/test-escaping-on-var-before-switch-case.c
new file mode 100644
index 00000000000000..95aed8cab06b55
--- /dev/null
+++ b/clang/test/Analysis/test-escaping-on-var-before-switch-case.c
@@ -0,0 +1,11 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config unroll-loops=true -verify %s
+
+void test_escaping_on_var_before_switch_case_no_crash(int c) {
+  switch (c) {
+    int i; // expected error{{Reached root without finding the declaration of VD}}
+    case 0: {
+      for (i = 0; i < 16; i++) {}
+      break;
+    }
+  }
+}



More information about the cfe-commits mailing list