Skip to content

Commit 177765b

Browse files
gwllxnv-cmeerw
authored andcommitted
Fix use of destroyed pack mapping in alias template CTAD [GH #533]
1 parent fdebe70 commit 177765b

7 files changed

Lines changed: 37 additions & 2 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[GH #533]
2+
Alias template CTAD with a constrained constructor of a variadic class template
3+
4+
Class template argument deduction through an alias template could read freed
5+
memory, and possibly crash, when a constructor of the underlying class
6+
template had a parameter pack and a requires clause, e.g. aliases of
7+
std::tuple with libstdc++. For example, with --c++20:
8+
9+
template<typename... E> struct S {
10+
static constexpr bool c = true;
11+
S(E...) requires c {}
12+
};
13+
template<typename... T> using A = S<T...>;
14+
A a{1};

‎src/templates.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45648,6 +45648,10 @@ On success, return the symbol for the new guide; otherwise return NULL.
4564845648
a_routine_ptr rout;
4564945649
a_ctws_state ctws_state;
4565045650
a_boolean copy_error = FALSE;
45651+
/* ctws_state.alias_parameter_pack_mapping points to one of these and is
45652+
still used for the requires clauses after the block below. */
45653+
Dyn_array<a_template_param_ptr>
45654+
pack_mapping, alias_pack_mapping;
4565145655

4565245656
if (guide_template_params != NULL) {
4565345657
a_symbol_ptr class_sym = symbol_for(ret_type)
@@ -45659,8 +45663,6 @@ On success, return the symbol for the new guide; otherwise return NULL.
4565945663
a_template_arg_ptr alias_proto_args;
4566045664
size_t number_of_guide_params = 0;
4566145665
Dyn_array<a_boolean> nondeduced_args, used_parameters;
45662-
Dyn_array<a_template_param_ptr>
45663-
pack_mapping, alias_pack_mapping;
4566445666

4566545667
/* Substitute the prototype arguments of the template alias into each of
4566645668
the deduced template arguments to record which template parameters of
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=2 --c++20 --g++ --gnu_version=150200 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=3 --c++20 --clang --clang_version=220100 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=4 --ms_c++20 --microsoft_version 1950 Test_name.c
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//type:fp
2+
//options:--c++20:--c++20 --g++ --gnu_version=150200:--c++20 --clang --clang_version=220100:--ms_c++20 --microsoft_version 1950
3+
4+
// Class template argument deduction through an alias template read a
5+
// destroyed array (the mapping of template parameter packs) when substituting
6+
// into the requires clause of a constructor of a class template with a
7+
// parameter pack, e.g. aliases of std::tuple with libstdc++.
8+
9+
template<typename... E> struct S
10+
{
11+
static constexpr bool c = true;
12+
S(E...) requires c {}
13+
};
14+
template<typename... T> using A = S<T...>;
15+
A a{1};

0 commit comments

Comments
 (0)