https://github.com/ojhunt created 
https://github.com/llvm/llvm-project/pull/219746

CheckAsmLabel fails to check for an incomplete type before attempting to get 
the type layout. Short circuit on an incomplete type as Sema will already 
reject a global with an incomplete type.

>From 5a494bca90d98bc51accc6bf8a5d7fb9c63f676a Mon Sep 17 00:00:00 2001
From: Oliver Hunt <[email protected]>
Date: Sat, 29 Aug 2026 00:14:45 -0600
Subject: [PATCH] [clang][Sema] Crash due to asm labeled global vars of
 undefined type

CheckAsmLabel fails to check for an incomplete type before attempting
to get the type layout. Short circuit on an incomplete type as Sema
will already reject a global with an incomplete type.
---
 clang/lib/Sema/SemaDecl.cpp                              | 4 ++++
 .../test/Sema/global-explicit-register-undefined-type.c  | 9 +++++++++
 2 files changed, 13 insertions(+)
 create mode 100644 clang/test/Sema/global-explicit-register-undefined-type.c

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 07c6157ab8f31..4850cb5a6cb9f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7756,6 +7756,10 @@ void Sema::CheckAsmLabel(Scope *S, Expr *E, StorageClass 
SC,
   StringLiteral *SE = cast<StringLiteral>(E);
   StringRef Label = SE->getString();
   QualType R = TInfo->getType();
+  if (RequireCompleteType(NewVD->getBeginLoc(), R, 
diag::err_typecheck_decl_incomplete_type)) {
+    NewVD->setInvalidDecl();
+    return;
+  }
   if (S->getFnParent() != nullptr) {
     switch (SC) {
     case SC_None:
diff --git a/clang/test/Sema/global-explicit-register-undefined-type.c 
b/clang/test/Sema/global-explicit-register-undefined-type.c
new file mode 100644
index 0000000000000..690dc0a9efddb
--- /dev/null
+++ b/clang/test/Sema/global-explicit-register-undefined-type.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 %s -triple arm64-apple-macosx -fsyntax-only -verify
+
+register struct Undefined1 bar asm("x1"); // #inline-type-def
+// expected-error@#inline-type-def {{variable has incomplete type 'struct 
Undefined1'}}
+// expected-note@#inline-type-def {{forward declaration of 'struct 
Undefined1'}}
+struct Undefined2; // #outline-type-def
+register struct Undefined2 bar asm("x1"); // #outline-type-label
+// expected-error@#outline-type-label {{variable has incomplete type 'struct 
Undefined2'}}
+// expected-note@#outline-type-def {{forward declaration of 'struct 
Undefined2'}}

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

Reply via email to