Skip to content

Commit f9be391

Browse files
authored
[LifetimeSafety] Handle pruned-edges (null blocks) in dataflow (#150670)
Fix a crash in the lifetime safety dataflow analysis when handling null CFG blocks. Added a null check for adjacent blocks in the dataflow analysis algorithm to prevent dereferencing null pointers. This occurs when processing CFG blocks with unreachable successors or predecessors. Original crash: https://compiler-explorer.com/z/qfzfqG5vM Fixes #150095
1 parent bc0f696 commit f9be391

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

clang/lib/Analysis/LifetimeSafety.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ class DataflowAnalysis {
603603
OutStates[B] = StateOut;
604604
Visited.set(B->getBlockID());
605605
for (const CFGBlock *AdjacentB : isForward() ? B->succs() : B->preds()) {
606+
if (!AdjacentB)
607+
continue;
606608
Lattice OldInState = getInState(AdjacentB);
607609
Lattice NewInState = D.join(OldInState, StateOut);
608610
// Enqueue the adjacent block if its in-state has changed or if we have

clang/unittests/Analysis/LifetimeSafetyTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,25 @@ TEST_F(LifetimeAnalysisTest, PointersAndExpirationInACycle) {
512512
EXPECT_THAT(LoansTo({"temp"}), AreExpiredAt("after_loop"));
513513
}
514514

515+
TEST_F(LifetimeAnalysisTest, InfiniteLoopPrunesEdges) {
516+
SetupTest(R"(
517+
void target(MyObj out) {
518+
MyObj *p = &out;
519+
POINT(before_loop);
520+
521+
for (;;) {
522+
POINT(begin);
523+
MyObj in;
524+
p = ∈
525+
POINT(end);
526+
}
527+
}
528+
)");
529+
EXPECT_THAT(Origin("p"), HasLoansTo({"out"}, "before_loop"));
530+
EXPECT_THAT(Origin("p"), HasLoansTo({"in", "out"}, "begin"));
531+
EXPECT_THAT(Origin("p"), HasLoansTo({"in"}, "end"));
532+
}
533+
515534
TEST_F(LifetimeAnalysisTest, NestedScopes) {
516535
SetupTest(R"(
517536
void target() {

0 commit comments

Comments
 (0)