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

Benedek Kaibas via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 12 14:52:54 PDT 2026


================
@@ -46,33 +47,31 @@ void ReportDanglingPtrDeref::checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
 
   if (const MemRegion *LocRegion = Loc.getAsRegion()) {
     if (lifetime_modeling::isDeallocated(State, LocRegion)) {
-      if (ExplodedNode *N =
-              C.generateNonFatalErrorNode(C.getState(), C.getPredecessor()))
-        reportUseAfterScope(LocRegion, N, C);
+      if (ExplodedNode *N = C.generateNonFatalErrorNode(State))
+        reportUseAfterScope(LocRegion, Loc, N, C);
     }
   }
 }
 
 void ReportDanglingPtrDeref::reportUseAfterScope(const MemRegion *Region,
-                                                 ExplodedNode *N,
+                                                 SVal Val, ExplodedNode *N,
                                                  CheckerContext &C) const {
   auto BR = std::make_unique<PathSensitiveBugReport>(
       BugMsg,
       (llvm::Twine("Use of '") + Region->getString() +
-       "' after its lifetime ended.")
-          .str(),
+       "' after its lifetime ended."),
       N);
+  BR->addVisitor(
+      std::make_unique<ReportDanglingPtrDerefBRVisitor>(Val, Region));
   C.emitReport(std::move(BR));
 }
 
 PathDiagnosticPieceRef
-ReportDanglingPtrDeref::ReportDanglingPtrDerefBRVisitor::VisitNode(
-    const ExplodedNode *N, BugReporterContext &BRC,
-    PathSensitiveBugReport &BR) {
-  if (!lifetime_modeling::isBoundToLifetimeSourceSet(N->getState(),
-                                                     BoundRegion) ||
-      lifetime_modeling::isBoundToLifetimeSourceSet(
-          N->getFirstPred()->getState(), BoundRegion))
+ReportDanglingPtrDerefBRVisitor::VisitNode(const ExplodedNode *N,
+                                           BugReporterContext &BRC,
+                                           PathSensitiveBugReport &BR) {
+  if (!isBoundToLifetimeSourceSet(N->getState(), BoundRegion) ||
+      isBoundToLifetimeSourceSet(N->getFirstPred()->getState(), BoundRegion))
----------------
benedekaibas wrote:

Applied changes here: [aa7e799](https://github.com/llvm/llvm-project/pull/206460/commits/aa7e79987d1a01e6cbc103973e6187dd27e6250f)

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


More information about the cfe-commits mailing list