[clang] [clang] Fix crash on invalid `std::initializer_list<T>` template-id (PR #132284)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 24 09:40:24 PDT 2025
https://github.com/offsetof updated https://github.com/llvm/llvm-project/pull/132284
>From 553ced1e367c0ec6399e71e7a3a3685a550f3431 Mon Sep 17 00:00:00 2001
From: offsetof <offsetof at mailo.com>
Date: Thu, 20 Mar 2025 20:54:45 +0000
Subject: [PATCH 1/2] [clang] Fix crash on invalid `std::initializer_list<T>`
template-id
In `Sema::BuildStdInitializerList`, check that the synthesized
template-id `std::initializer_list<T>` is valid (which might not be the
case if the template has associated constraints or subsequent parameters
with default arguments) before forming the type.
---
clang/lib/Sema/SemaDeclCXX.cpp | 8 ++++++--
.../test/SemaCXX/invalid-std-initializer-list.cpp | 14 ++++++++++++++
2 files changed, 20 insertions(+), 2 deletions(-)
create mode 100644 clang/test/SemaCXX/invalid-std-initializer-list.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 7b2e0df8cb55d..54f7bc9a3b021 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -12182,10 +12182,14 @@ QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) {
Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element),
Context.getTrivialTypeSourceInfo(Element,
Loc)));
+
+ QualType T = CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args);
+ if (T.isNull())
+ return QualType();
+
return Context.getElaboratedType(
ElaboratedTypeKeyword::None,
- NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()),
- CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args));
+ NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()), T);
}
bool Sema::isInitListConstructor(const FunctionDecl *Ctor) {
diff --git a/clang/test/SemaCXX/invalid-std-initializer-list.cpp b/clang/test/SemaCXX/invalid-std-initializer-list.cpp
new file mode 100644
index 0000000000000..080a712759c45
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-std-initializer-list.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 %s -verify -std=c++20
+
+namespace std {
+
+template<class T, class = T::x> // expected-error 2 {{type 'int' cannot be used prior to '::' because it has no members}}
+class initializer_list;
+
+}
+
+auto x = {1}; // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
+
+void f() {
+ for(int x : {1, 2}); // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
+}
>From bd5c32e5716d32a0134d99a6b16bb5cd3039f655 Mon Sep 17 00:00:00 2001
From: offsetof <offsetof at mailo.com>
Date: Mon, 24 Mar 2025 15:58:36 +0000
Subject: [PATCH 2/2] fixup! [clang] Fix crash on invalid
`std::initializer_list<T>` template-id
---
clang/docs/ReleaseNotes.rst | 1 +
clang/test/SemaCXX/invalid-std-initializer-list.cpp | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 159991e8db981..f1b4310134702 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -357,6 +357,7 @@ Bug Fixes to C++ Support
- Fixed a Clang regression in C++20 mode where unresolved dependent call expressions were created inside non-dependent contexts (#GH122892)
- Clang now emits the ``-Wunused-variable`` warning when some structured bindings are unused
and the ``[[maybe_unused]]`` attribute is not applied. (#GH125810)
+- Fixed a crash caused by invalid declarations of ``std::initializer_list``. (#GH132256)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/SemaCXX/invalid-std-initializer-list.cpp b/clang/test/SemaCXX/invalid-std-initializer-list.cpp
index 080a712759c45..93246b5f03fd4 100644
--- a/clang/test/SemaCXX/invalid-std-initializer-list.cpp
+++ b/clang/test/SemaCXX/invalid-std-initializer-list.cpp
@@ -7,8 +7,12 @@ class initializer_list;
}
+namespace gh132256 {
+
auto x = {1}; // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
void f() {
for(int x : {1, 2}); // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
}
+
+}
More information about the cfe-commits
mailing list