[clang] [Clang] Fix crash with large pack indexing indices (PR #247923)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 2 03:05:47 PST 2026


https://github.com/YzK0741 updated https://github.com/llvm/llvm-project/pull/247923

>From 53d26b46adb5a610cdc8d100dc864971b479eb1c Mon Sep 17 00:00:00 2001
From: YzK0741 <2353036486 at qq.com>
Date: Wed, 2 Dec 2026 12:31:16 +0800
Subject: [PATCH 1/3] [Clang] Fix crash with large pack indexing indices

PackIndexingType::getSelectedIndex() used getExtValue(), which asserts for
indices that do not fit in int64_t, and BuildPackIndexingType() returned
early for such values before they could be diagnosed. Compare the index
against the pack size using the APSInt value directly, and only select an
index once the pack is fully substituted.

Fixes #246835
---
 clang/lib/AST/Type.cpp                     |  4 ++--
 clang/lib/Sema/SemaType.cpp                | 21 +++++++++++++--------
 clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 15 +++++++++++++++
 3 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4e4c4a37ecff71..2c386926049d86 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -4533,7 +4533,7 @@ PackIndexingType::PackIndexingType(QualType Canonical, QualType Pattern,
 }
 
 UnsignedOrNone PackIndexingType::getSelectedIndex() const {
-  if (isInstantiationDependentType())
+  if (!isFullySubstituted() || isInstantiationDependentType())
     return std::nullopt;
   // Should only be not a constant for error recovery.
   ConstantExpr *CE = dyn_cast<ConstantExpr>(getIndexExpr());
@@ -4541,7 +4541,7 @@ UnsignedOrNone PackIndexingType::getSelectedIndex() const {
     return std::nullopt;
   auto Index = CE->getResultAsAPSInt();
   assert(Index.isNonNegative() && "Invalid index");
-  return static_cast<unsigned>(Index.getExtValue());
+  return static_cast<unsigned>(Index.getZExtValue());
 }
 
 TypeDependence
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index de8ab4066aaa29..df947e0a245b52 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -10392,21 +10392,26 @@ QualType Sema::BuildPackIndexingType(QualType Pattern, Expr *IndexExpr,
 
   UnsignedOrNone Index = std::nullopt;
   if (!IndexExpr->isInstantiationDependent()) {
+    DiagnosticErrorTrap ErrorTrap(getDiagnostics());
     llvm::APSInt Value;
     ExprResult Res = CheckConvertedConstantExpression(
         IndexExpr, Context.getSizeType(), Value, CCEKind::PackIndex);
-
-    if (!Res.isUsable() || !Value.isRepresentableByInt64())
+    if (!Res.isUsable())
       return QualType();
 
     IndexExpr = Res.get();
-    uint64_t V = Value.getZExtValue();
-    if (FullySubstituted && V >= Expansions.size()) {
-      Diag(IndexExpr->getBeginLoc(), diag::err_pack_index_out_of_bound)
-          << V << Pattern << Expansions.size();
-      return QualType();
+    if (FullySubstituted) {
+      // A narrowing conversion may have diagnosed an error and still returned
+      // a value for recovery.
+      if (ErrorTrap.hasErrorOccurred())
+        return QualType();
+      if (Value.uge(Expansions.size())) {
+        Diag(IndexExpr->getBeginLoc(), diag::err_pack_index_out_of_bound)
+            << Value.getZExtValue() << Pattern << Expansions.size();
+        return QualType();
+      }
+      Index = static_cast<unsigned>(Value.getZExtValue());
     }
-    Index = static_cast<unsigned>(V);
   }
 
   return Context.getPackIndexingType(Pattern, IndexExpr, FullySubstituted,
diff --git a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
index 910160dbf683d9..8a3e183eda229f 100644
--- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
+++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
@@ -404,3 +404,18 @@ void test() {
   // expected-note@#GH232220-f {{candidate template ignored: substitution failure [with T = <int>]: invalid index 1 for pack 'T' of size 1}}
 }
 } // namespace GH232220
+
+namespace GH246835 {
+// A pack index that does not fit in a 32-bit value used to crash the compiler;
+// it must be diagnosed once the pack is expanded.
+template <typename... T> auto f(T...[0x8000000000000000ULL]);
+
+template <typename... T> using A = T...[0x8000000000000000ULL]; // expected-error {{invalid index 9223372036854775808 for pack 'T' of size 1}}
+using B = A<int>; // expected-note {{in instantiation of template type alias 'A' requested here}}
+
+template <typename... T> using C = T...[0xffffffffULL]; // expected-error {{invalid index 4294967295 for pack 'T' of size 1}}
+using D = C<int>; // expected-note {{in instantiation of template type alias 'C' requested here}}
+
+template <typename... T> using E = T...[0];
+using F = E<int>;
+}

>From 812fdf3b3c0f7376a31cc4950bc97626a1f7c615 Mon Sep 17 00:00:00 2001
From: YzK0741 <2353036486 at qq.com>
Date: Wed, 2 Dec 2026 17:15:09 +0800
Subject: [PATCH 2/3] Add release note for #246835

---
 clang/docs/ReleaseNotes.md | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 676d2b2761c2fa..ea4ef5c9634cd1 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1101,6 +1101,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   type. (#GH204756)
 
 #### Bug Fixes to C++ Support
+
+- Fixed a crash when indexing a C++ template type parameter pack with a large
+  constant index. Clang now diagnoses out-of-bounds indices once the pack is
+  fully substituted. (#GH246835)
+
 - Clang now recovers from invalid single pointer declarators in structured
   bindings (such as ``auto *[x, y] = s;``) by removing the pointer declarator.
   This allows type deduction to proceed and avoids redundant follow-up

>From 53cd0c45884afed9c387d5f30995df9b43a04ff9 Mon Sep 17 00:00:00 2001
From: YzK0741 <2353036486 at qq.com>
Date: Wed, 2 Dec 2026 19:05:15 +0800
Subject: [PATCH 3/3] Drop the isFullySubstituted() check in getSelectedIndex()

---
 clang/lib/AST/Type.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 2c386926049d86..0f03ef67ee2291 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -4533,7 +4533,7 @@ PackIndexingType::PackIndexingType(QualType Canonical, QualType Pattern,
 }
 
 UnsignedOrNone PackIndexingType::getSelectedIndex() const {
-  if (!isFullySubstituted() || isInstantiationDependentType())
+  if (isInstantiationDependentType())
     return std::nullopt;
   // Should only be not a constant for error recovery.
   ConstantExpr *CE = dyn_cast<ConstantExpr>(getIndexExpr());



More information about the cfe-commits mailing list