Skip to content

Commit f8b7964

Browse files
Fix #11146 Crash in expandTemplate() (#4238)
1 parent a2f2699 commit f8b7964

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

lib/templatesimplifier.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3791,8 +3791,10 @@ void TemplateSimplifier::simplifyTemplates(
37913791
specializations,
37923792
maxtime,
37933793
expandedtemplates);
3794-
if (instantiated)
3794+
if (instantiated) {
37953795
mInstantiatedTemplates.push_back(*iter1);
3796+
mTemplateNamePos.clear(); // positions might be invalid after instantiations
3797+
}
37963798
}
37973799

37983800
for (std::list<TokenAndName>::const_iterator it = mInstantiatedTemplates.begin(); it != mInstantiatedTemplates.end(); ++it) {

test/testsimplifytemplate.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class TestSimplifyTemplate : public TestFixture {
218218
TEST_CASE(template173); // #10332 crash
219219
TEST_CASE(template174); // #10506 hang
220220
TEST_CASE(template175); // #10908
221+
TEST_CASE(template176); // #11146
221222
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
222223
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
223224
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@@ -4458,7 +4459,7 @@ class TestSimplifyTemplate : public TestFixture {
44584459
ASSERT_EQUALS(exp, tok(code));
44594460
}
44604461

4461-
void template175()
4462+
void template175() // #10908
44624463
{
44634464
const char code[] = "template <typename T, int value> T Get() {return value;}\n"
44644465
"char f() { Get<int,10>(); }\n";
@@ -4468,6 +4469,27 @@ class TestSimplifyTemplate : public TestFixture {
44684469
ASSERT_EQUALS(exp, tok(code));
44694470
}
44704471

4472+
void template176() // #11146 don't crash
4473+
{
4474+
const char code[] = "struct a {\n"
4475+
" template <typename> class b {};\n"
4476+
"};\n"
4477+
"struct c {\n"
4478+
" template <typename> a::b<int> d();\n"
4479+
" ;\n"
4480+
"};\n"
4481+
"template <typename> a::b<int> c::d() {}\n"
4482+
"template <> class a::b<int> c::d<int>() { return {}; };\n";
4483+
const char exp[] = "struct a { "
4484+
"class b<int> c :: d<int> ( ) ; "
4485+
"template < typename > class b { } ; "
4486+
"} ; "
4487+
"struct c { a :: b<int> d<int> ( ) ; } ; "
4488+
"class a :: b<int> c :: d<int> ( ) { return { } ; } ; "
4489+
"a :: b<int> c :: d<int> ( ) { }";
4490+
ASSERT_EQUALS(exp, tok(code));
4491+
}
4492+
44714493
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
44724494
const char code[] = "template <typename T> struct C {};\n"
44734495
"template <typename T> struct S {a};\n"

0 commit comments

Comments
 (0)