[clang] [analyzer] Implemented the DanglingPtrDeref checker to detect use-after-scope lifetime errors (PR #209278)

Balázs Benics via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 14 04:56:41 PDT 2026


================
@@ -0,0 +1,86 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.ReportDanglingPtrDeref \
+// RUN:   -analyzer-config cfg-lifetime=true -analyzer-output=text -verify %s
+
+void test_case_one() {
+  int *ptr = nullptr;
+  {
+    int num = 5;
+    ptr = #
+  }
+  // expected-note at -1 {{'num' is destroyed here}}
+  *ptr = 6;
+  // expected-warning at -1 {{Use of 'num' after its lifetime ended}}
+  // expected-note at -2    {{Use of 'num' after its lifetime ended}}
+}
+
+void test_case_two() {
+  int *ptr_one = nullptr;
+  int *ptr_two = nullptr;
+  {
+    int n = 1;
+    int m = 2;
+    ptr_one = &n;
+    ptr_two = &m;
+  }
+  // expected-note at -1 {{'n' is destroyed here}}
+  // expected-note at -2 {{'m' is destroyed here}}
+  *ptr_one = 6;
+  *ptr_two = 7; 
+  // expected-warning at -2 {{Use of 'n' after its lifetime ended}}
+  // expected-note at -3    {{Use of 'n' after its lifetime ended}}
----------------
steakhal wrote:

The only way you can surface (thus test) BR visitor notes if you use `text` mode. The downside of using `text` mode is that the warnings also appear as the last note piece which is intentional. It may look redundant but it is not in non-cli tools, such as Xcode. They appear at different places.

https://github.com/llvm/llvm-project/pull/209278


More information about the cfe-commits mailing list