llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Gábor Horváth (Xazax-hun)

<details>
<summary>Changes</summary>

A pruned CFG edge, such as the false branch of `if (6)`, shows up as a null 
predecessor. Since the dataflow states are indexed by block ID, looking up the 
out-state of that null block crashes. Skip it, as the other predecessor walks 
already do.

Fixes a regression from #<!-- -->246491.

Assisted-by: Claude Opus 5.5

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


2 Files Affected:

- (modified) clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp (+2) 
- (modified) clang/test/Sema/LifetimeSafety/dangling-global.cpp (+9-1) 


``````````diff
diff --git a/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp 
b/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
index 58979bbcfd5cee..732274d2e1f730 100644
--- a/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
@@ -220,6 +220,8 @@ class AnalysisImpl
       // Only explore predecessor blocks where the target loan is present in 
the
       // current origin.
       for (const CFGBlock *PredBlock : CurrBlock->preds()) {
+        if (!PredBlock)
+          continue;
         SearchState NextState = {PredBlock, CurrOID};
         if (getLoans(getOutState(PredBlock), CurrOID).contains(TargetLoan) &&
             VistedStates.insert(NextState).second)
diff --git a/clang/test/Sema/LifetimeSafety/dangling-global.cpp 
b/clang/test/Sema/LifetimeSafety/dangling-global.cpp
index 0b4f79a562d031..2d3bf2638c8aee 100644
--- a/clang/test/Sema/LifetimeSafety/dangling-global.cpp
+++ b/clang/test/Sema/LifetimeSafety/dangling-global.cpp
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wno-dangling -verify %s
 #include "Inputs/lifetime-analysis.h"
 
-int *global; // expected-note 11 {{this global dangles}}
+int *global; // expected-note 12 {{this global dangles}}
 int *global_backup; // expected-note {{this global dangles}}
 
 std::string_view global_view; // expected-note 2 {{this global dangles}}
@@ -194,6 +194,14 @@ void store_local_in_global_but_moved(std::string s){
   takeString(std::move(s)); //expected-note {{potentially moved here}}
 }
 
+// The constant condition prunes the false edge, leaving a null predecessor.
+void store_local_in_global_under_constant_condition() {
+  if (6) {
+    int local;
+    global = &local; // expected-warning {{stack memory associated with local 
variable 'local' escapes to the global variable 'global' which will dangle}}
+  }
+}
+
 int main() {
   int local = 0;
   global = &local; // expected-warning {{stack memory associated with local 
variable 'local' escapes to the global variable 'global' which will dangle 
after main returns}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/246581
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to