Skip to content

Conversation

steakhal
Copy link
Contributor

Basically, the issue was that we should have unwrap the base region before we special handle temp object regions.

Fixes #66221

I also decided to add some extra range information to the diagnostics to make it consistent with the other reporting path.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html labels Sep 15, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2023

@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Changes Basically, the issue was that we should have unwrap the base region before we special handle temp object regions.

Fixes #66221

I also decided to add some extra range information to the diagnostics to make it consistent with the other reporting path.

Full diff: https://github.com/llvm/llvm-project/pull/66493.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp (+4-2)
  • (added) clang/test/Analysis/stackaddrleak.cpp (+24)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
index 19ff8c8e2a171ae..23a774931b21dec 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
@@ -369,7 +369,7 @@ void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS,
                                   "Stack address stored into global variable");
 
   for (const auto &P : Cb.V) {
-    const MemRegion *Referrer = P.first;
+    const MemRegion *Referrer = P.first->getBaseRegion();
     const MemRegion *Referred = P.second;
 
     // Generate a report for this bug.
@@ -384,6 +384,8 @@ void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS,
           << CommonSuffix;
       auto Report =
           std::make_unique<PathSensitiveBugReport>(*BT_stackleak, Out.str(), N);
+      if (Range.isValid())
+        Report->addRange(Range);
       Ctx.emitReport(std::move(Report));
       return;
     }
@@ -398,7 +400,7 @@ void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS,
     }(Referrer->getMemorySpace());
 
     // This cast supposed to succeed.
-    const VarRegion *ReferrerVar = cast<VarRegion>(Referrer->getBaseRegion());
+    const auto *ReferrerVar = cast<VarRegion>(Referrer);
     const std::string ReferrerVarName =
         ReferrerVar->getDecl()->getDeclName().getAsString();
 
diff --git a/clang/test/Analysis/stackaddrleak.cpp b/clang/test/Analysis/stackaddrleak.cpp
new file mode 100644
index 000000000000000..5828f2ac6e78c8d
--- /dev/null
+++ b/clang/test/Analysis/stackaddrleak.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
+
+void *operator new(unsigned long, void *p) { return p; }
+
+struct myfunction {
+  union storage_t {
+    char buffer[100];
+    unsigned long long max_align;
+  } storage;
+
+  template <typename Func> myfunction(Func fn) {
+    new (&storage.buffer) Func(fn);
+  }
+  void operator()();
+};
+
+myfunction create_func() {
+  int n;
+  auto c = [&n] {};
+  return c; // expected-warning {{Address of stack memory associated with local variable 'n' is still referred to by a temporary object on the stack upon returning to the caller.  This will be a dangling reference}}
+}
+void gh_66221() {
+  create_func()();
+}

Copy link
Contributor

@NagyDonat NagyDonat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice bugfix; I have only one tangential remark.

@steakhal
Copy link
Contributor Author

Let me know if you are still okay with the latest change. @donatnagye @Xazax-hun

@NagyDonat
Copy link
Contributor

LGTM.

Basically, the issue was that we should have unwrap the base region
before we special handle temp object regions.

Fixes #66221
@steakhal
Copy link
Contributor Author

Fixed tests for Windows, to use size_t for operator new.
I'll land this if premerge-tests pass.

@steakhal steakhal merged commit 73dcbd4 into llvm:main Sep 20, 2023
@steakhal steakhal deleted the fix-gh-66221 branch September 20, 2023 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[static analyser][StackAddrEscapeChecker] crash with std::function capturing a dangling reference
5 participants