[clang] [Clang] fix crash due to incorrect argument position in merging deduced template arguments (PR #118041)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 28 16:57:59 PST 2024


https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/118041

Fixes #113659 

>From 59fa1de4e6638f1c8bba39e2e55c43cb3991fc3c Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 29 Nov 2024 02:54:53 +0200
Subject: [PATCH] [Clang] fix crash due to incorrect argument position in
 merging deduced template arguments

---
 clang/docs/ReleaseNotes.rst                          |  1 +
 clang/lib/Sema/SemaTemplateDeduction.cpp             |  3 ++-
 .../template-args-deduction-aggregates.cpp           | 12 ++++++++++++
 3 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaTemplate/template-args-deduction-aggregates.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2ecd19bdc39448..7e78e7eb9ec0a3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -760,6 +760,7 @@ Bug Fixes to C++ Support
   missing placeholder return type. (#GH78694)
 - Fixed a bug where bounds of partially expanded pack indexing expressions were checked too early. (#GH116105)
 - Fixed an assertion failure caused by using ``consteval`` in condition in consumed analyses. (#GH117385)
+- Fix a crash caused by incorrect argument position in merging deduced template arguments. (#GH113659)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 62a0c30d995020..fad20b37a7d9a2 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -376,7 +376,7 @@ checkDeducedTemplateArguments(ASTContext &Context,
     for (TemplateArgument::pack_iterator
              XA = X.pack_begin(),
              XAEnd = X.pack_end(), YA = Y.pack_begin(), YAEnd = Y.pack_end();
-         XA != XAEnd; ++XA, ++YA) {
+         XA != XAEnd; ++XA) {
       if (YA != YAEnd) {
         TemplateArgument Merged = checkDeducedTemplateArguments(
             Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
@@ -384,6 +384,7 @@ checkDeducedTemplateArguments(ASTContext &Context,
         if (Merged.isNull() && !(XA->isNull() && YA->isNull()))
           return DeducedTemplateArgument();
         NewPack.push_back(Merged);
+        ++YA;
       } else {
         NewPack.push_back(*XA);
       }
diff --git a/clang/test/SemaTemplate/template-args-deduction-aggregates.cpp b/clang/test/SemaTemplate/template-args-deduction-aggregates.cpp
new file mode 100644
index 00000000000000..afe78d0873bb33
--- /dev/null
+++ b/clang/test/SemaTemplate/template-args-deduction-aggregates.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++20
+
+// expected-no-diagnostics
+
+namespace GH113659 {
+template <class... Args> struct S {};
+struct T {};
+struct U {};
+
+template <class... Args> struct B : S<Args...>, Args... {};
+B b{S<T, U>{}};
+}



More information about the cfe-commits mailing list