diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index a8c167d1f..1869b446c 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1024,7 +1024,14 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration) std::map typeParameterNames; // Scan template declaration.. - for (Token *tok = declaration.token(); tok; tok = tok->next()) { + for (Token *tok = declaration.token()->next(); tok; tok = tok->next()) { + if (Token::simpleMatch(tok, "template <")) { + Token* end = tok->next()->findClosingBracket(); + if (end) + tok = end; + continue; + } + if (tok->link() && Token::Match(tok, "{|(|[")) { // Ticket #6835 tok = tok->link(); continue; @@ -1796,8 +1803,12 @@ void TemplateSimplifier::expandTemplate( if (Token::Match(start, "[|{|(")) { links[start->link()] = dst->previous(); } else if (Token::Match(start, "]|}|)")) { - Token::createMutualLinks(links[start], dst->previous()); - links.erase(start); + std::map::iterator link = links.find(start); + // make sure link is valid + if (link != links.end()) { + Token::createMutualLinks(link->second, dst->previous()); + links.erase(start); + } } } } diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 954674356..c88bb8a6e 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -214,6 +214,7 @@ private: TEST_CASE(template170); // crash TEST_CASE(template171); // crash TEST_CASE(template172); // #10258 crash + TEST_CASE(template173); // #10332 crash TEST_CASE(template_specialization_1); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_specialization_2); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template) @@ -4416,6 +4417,29 @@ private: ASSERT_EQUALS(exp, tok(code)); } + void template173() { // #10332 crash + const char code[] = "namespace a {\n" + "template struct b;\n" + "template