[clang] [analyzer] MallocChecker – Fix false positive leak for smart pointers in temporary objects (PR #152751)

Donát Nagy via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 29 05:40:27 PDT 2025


================
@@ -3068,12 +3124,225 @@ void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
   C.addTransition(state->set<RegionState>(RS), N);
 }
 
+// Helper function to check if a name is a recognized smart owning pointer name
+static bool isSmartOwningPtrName(StringRef Name) {
+  return Name == "unique_ptr" || Name == "shared_ptr";
+}
+
+// Allowlist of owning smart pointers we want to recognize.
+// Start with unique_ptr and shared_ptr. (intentionally exclude weak_ptr)
+static bool isSmartOwningPtrType(QualType QT) {
+  QT = QT->getCanonicalTypeUnqualified();
+
+  // First try TemplateSpecializationType (for both std and custom smart
+  // pointers)
+  if (const auto *TST = QT->getAs<TemplateSpecializationType>()) {
+    const TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl();
+    if (!TD)
+      return false;
+
+    const auto *ND = dyn_cast_or_null<NamedDecl>(TD->getTemplatedDecl());
+    if (!ND)
+      return false;
+
+    // Accept both std and custom smart pointer implementations for broader
+    // coverage
----------------
NagyDonat wrote:

```suggestion
    // For broader coverage we recognize all template classes with names that
    // match the allowlist even if they are not declared in namespace 'std'.
```
This comment became a bit unclear now that nothing else references the question of belonging to namespace `std`.

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


More information about the cfe-commits mailing list