Fix #9471 FP unreadVariable caused by invalid template injection (#3783)

This commit is contained in:
chrchr-github 2022-02-01 17:15:27 +01:00 committed by GitHub
parent 842f2b2da7
commit 127b3bb1c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View File

@ -2141,7 +2141,7 @@ void TemplateSimplifier::expandTemplate(
addNamespace(templateDeclaration, tok3); addNamespace(templateDeclaration, tok3);
} }
mTokenList.addtoken(newName, tok3); mTokenList.addtoken(newName, tok3);
} else if (!Token::Match(tok3->next(), ":|{|=|;")) } else if (!Token::Match(tok3->next(), ":|{|=|;|["))
tok3->str(newName); tok3->str(newName);
continue; continue;
} }

View File

@ -5561,6 +5561,24 @@ private:
"class C<int> { } ;"; "class C<int> { } ;";
ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS(expected, tok(code));
} }
{
const char code[] = "class A {};\n"
"template<typename T> struct B;\n"
"template<> struct B<A> {};\n"
"int f() {\n"
" int B[1] = {};\n"
" return B[0];\n"
"}\n";
const char expected[] = "class A { } ; "
"struct B<A> ; "
"template < typename T > struct B ; "
"struct B<A> { } ; "
"int f ( ) { "
"int B [ 1 ] = { } ; "
"return B [ 0 ] ; "
"}";
ASSERT_EQUALS(expected, tok(code));
}
} }
void templateAlias1() { void templateAlias1() {

View File

@ -5781,6 +5781,17 @@ private:
" std::array<int, 1> a;\n" " std::array<int, 1> a;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: a\n", errout.str());
functionVariableUsage("class A {};\n" // #9471
" namespace std {\n"
" template<>\n"
" struct hash<A> {};\n"
"}\n"
"char f() {\n"
" std::string hash = \"-\";\n"
" return hash[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void localvarFuncPtr() { void localvarFuncPtr() {