https://github.com/nataliakokoromyti created 
https://github.com/llvm/llvm-project/pull/175415

When an asm goto statement references an undeclared label and there's a 
variable with __attribute__((cleanup)) in scope, clang would crash with a 
segmentation fault.

The issue was that DiagnoseIndirectOrAsmJumpStmt() called 
Target->getStmt()->getIdentLoc() without checking if getStmt() returns null. 
For undeclared labels, the LabelDecl exists but has no associated LabelStmt.

This patch adds a null check and falls back to Target->getLocation() when the 
statement is null.

Fixes #175314

>From 9af9ed3356707a501b658308f4cd695746874f49 Mon Sep 17 00:00:00 2001
From: Natalia Kokoromyti <[email protected]>
Date: Sat, 10 Jan 2026 21:54:11 -0800
Subject: [PATCH] [Sema] Fix crash in asm goto with undeclared label

When an asm goto statement references an undeclared label and there's
a variable with __attribute__((cleanup)) in scope, clang would crash
with a segmentation fault.

The issue was that DiagnoseIndirectOrAsmJumpStmt() called
Target->getStmt()->getIdentLoc() without checking if getStmt() returns
null. For undeclared labels, the LabelDecl exists but has no associated
LabelStmt.

This patch adds a null check and falls back to Target->getLocation()
when the statement is null.

Fixes #175314
---
 clang/lib/Sema/JumpDiagnostics.cpp                |  7 +++++--
 clang/test/Sema/asm-goto-undeclared-label-crash.c | 12 ++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Sema/asm-goto-undeclared-label-crash.c

diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index 36c9d9afb37f1..b630559c2db2c 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -914,8 +914,11 @@ static void DiagnoseIndirectOrAsmJumpStmt(Sema &S, Stmt 
*Jump,
   bool IsAsmGoto = isa<GCCAsmStmt>(Jump);
   S.Diag(Jump->getBeginLoc(), diag::err_indirect_goto_in_protected_scope)
       << IsAsmGoto;
-  S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target)
-      << IsAsmGoto;
+  // Target->getStmt() can be null for undeclared labels.
+  SourceLocation TargetLoc = Target->getStmt()
+                                 ? Target->getStmt()->getIdentLoc()
+                                 : Target->getLocation();
+  S.Diag(TargetLoc, diag::note_indirect_goto_target) << IsAsmGoto;
   Diagnosed = true;
 }
 
diff --git a/clang/test/Sema/asm-goto-undeclared-label-crash.c 
b/clang/test/Sema/asm-goto-undeclared-label-crash.c
new file mode 100644
index 0000000000000..8538144a87a7d
--- /dev/null
+++ b/clang/test/Sema/asm-goto-undeclared-label-crash.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// Test that we don't crash when an asm goto references an undeclared label
+// and there's a variable with __attribute__((cleanup)) in scope.
+// See: https://github.com/llvm/llvm-project/issues/175314
+
+void a(int *b) {
+  int __attribute__((cleanup(a))) c = 0; // expected-note {{jump exits scope 
of variable with __attribute__((cleanup))}}
+  __asm__ goto("" : : : : d); // expected-error {{use of undeclared label 
'd'}} \
+                              // expected-error {{cannot jump from this asm 
goto statement to one of its possible targets}} \
+                              // expected-note {{possible target of asm goto 
statement}}
+}

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

Reply via email to