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

Fix assertion failure 'getType() == Other.getType()' in 
TypeLoc::initializeFullCopy when parsing malformed _Atomic types with nested 
sizeof/alignof expressions.

When error recovery produces a type that differs from the original 
TypeSourceInfo, we now fall back to initialize() instead of 
initializeFullCopy() to avoid the assertion failure.

Fixes #173886

>From 3e8c13944d1905b2c38f870e5ad3eb3574e328c4 Mon Sep 17 00:00:00 2001
From: Natalia Kokoromyti <[email protected]>
Date: Tue, 6 Jan 2026 05:04:03 -0800
Subject: [PATCH] [Sema] Fix crash in TypeLoc::initializeFullCopy with
 mismatched types

Fix assertion failure 'getType() == Other.getType()' in
TypeLoc::initializeFullCopy when parsing malformed _Atomic types
with nested sizeof/alignof expressions.

When error recovery produces a type that differs from the original
TypeSourceInfo, we now fall back to initialize() instead of
initializeFullCopy() to avoid the assertion failure.

Fixes #173886
---
 clang/lib/Sema/SemaType.cpp                  | 13 +++++++++++--
 clang/test/Sema/atomic-type-mismatch-crash.c | 10 ++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Sema/atomic-type-mismatch-crash.c

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 7ef83433326ed..bf8bc6e9d82a4 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6138,7 +6138,11 @@ namespace {
         TypeSourceInfo *TInfo = nullptr;
         Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
         assert(TInfo);
-        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
+        if (TL.getValueLoc().getType() == TInfo->getTypeLoc().getType())
+          TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
+        else
+          TL.getValueLoc().initialize(Context,
+                                      TInfo->getTypeLoc().getBeginLoc());
       } else {
         TL.setKWLoc(DS.getAtomicSpecLoc());
         // No parens, to indicate this was spelled as an _Atomic qualifier.
@@ -6152,7 +6156,12 @@ namespace {
 
       TypeSourceInfo *TInfo = nullptr;
       Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
-      TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
+      if (TInfo && TL.getValueLoc().getType() == TInfo->getTypeLoc().getType())
+        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
+      else
+        TL.getValueLoc().initialize(Context,
+                                    TInfo ? TInfo->getTypeLoc().getBeginLoc()
+                                          : DS.getTypeSpecTypeLoc());
     }
 
     void VisitExtIntTypeLoc(BitIntTypeLoc TL) {
diff --git a/clang/test/Sema/atomic-type-mismatch-crash.c 
b/clang/test/Sema/atomic-type-mismatch-crash.c
new file mode 100644
index 0000000000000..03c1b1f2ca54d
--- /dev/null
+++ b/clang/test/Sema/atomic-type-mismatch-crash.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// This test checks that we don't crash when parsing malformed _Atomic types
+// with nested sizeof/alignof expressions. See GitHub issue #173886.
+
+int a[100];
+int main() {
+  a[__alignof__(_Atomic(void) _Atomic double unsigned)]; // expected-error 
{{cannot combine with previous '_Atomic' declaration specifier}} \
+                                                         // expected-error 
{{'_Atomic' cannot be signed or unsigned}} \
+                                                         // expected-error 
{{_Atomic cannot be applied to incomplete type 'void'}}
+}

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

Reply via email to