[clang] [LifetimeSafety] Skip unreachable predecessors when building origin flow chains (PR #246581)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 00:44:25 PST 2026
https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/246581
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
>From a6df80d339be9fbdafe370f57f138acae1e70a38 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Fri, 27 Nov 2026 08:36:38 +0000
Subject: [PATCH] [LifetimeSafety] Skip unreachable predecessors when building
origin flow chains
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
---
clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp | 2 ++
clang/test/Sema/LifetimeSafety/dangling-global.cpp | 10 +++++++++-
2 files changed, 11 insertions(+), 1 deletion(-)
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}}
More information about the cfe-commits
mailing list