fix #10332 (cppcheck crashes) (#3371)

This commit is contained in:
Robert Reif 2021-08-01 04:31:36 -04:00 committed by GitHub
parent 2f6a6e4b45
commit 94dc6c2c3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -1024,7 +1024,14 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration)
std::map<std::string, unsigned int> 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<const Token *, Token *>::iterator link = links.find(start);
// make sure link is valid
if (link != links.end()) {
Token::createMutualLinks(link->second, dst->previous());
links.erase(start);
}
}
}
}

View File

@ -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 <typename T> struct S<C<T>> {..};
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
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 <typename, typename> struct b;\n"
"template <template <typename, typename> class = b> class c;\n"
"using d = c<>;\n"
"template <template <typename, typename = void> class> class c {};\n"
"}\n"
"namespace std {\n"
"template <> void swap<a::d>(a::d &, a::d &) {}\n"
"}";
const char exp[] = "namespace a { "
"template < typename , typename > struct b ; "
"template < template < typename , typename > class > class c ; "
"class c<b> ; "
"} "
"namespace std { "
"void swap<a::c<b>> ( a :: c<b> & , a :: c<b> & ) ; "
"void swap<a::c<b>> ( a :: c<b> & , a :: c<b> & ) { } "
"} "
"class a :: c<b> { } ;";
ASSERT_EQUALS(exp, tok(code));
}
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
const char code[] = "template <typename T> struct C {};\n"
"template <typename T> struct S {a};\n"