Skip to content

Commit 127b3bb

Browse files
Fix #9471 FP unreadVariable caused by invalid template injection (#3783)
1 parent 842f2b2 commit 127b3bb

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@ void TemplateSimplifier::expandTemplate(
21412141
addNamespace(templateDeclaration, tok3);
21422142
}
21432143
mTokenList.addtoken(newName, tok3);
2144-
} else if (!Token::Match(tok3->next(), ":|{|=|;"))
2144+
} else if (!Token::Match(tok3->next(), ":|{|=|;|["))
21452145
tok3->str(newName);
21462146
continue;
21472147
}

test/testsimplifytemplate.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5561,6 +5561,24 @@ class TestSimplifyTemplate : public TestFixture {
55615561
"class C<int> { } ;";
55625562
ASSERT_EQUALS(expected, tok(code));
55635563
}
5564+
{
5565+
const char code[] = "class A {};\n"
5566+
"template<typename T> struct B;\n"
5567+
"template<> struct B<A> {};\n"
5568+
"int f() {\n"
5569+
" int B[1] = {};\n"
5570+
" return B[0];\n"
5571+
"}\n";
5572+
const char expected[] = "class A { } ; "
5573+
"struct B<A> ; "
5574+
"template < typename T > struct B ; "
5575+
"struct B<A> { } ; "
5576+
"int f ( ) { "
5577+
"int B [ 1 ] = { } ; "
5578+
"return B [ 0 ] ; "
5579+
"}";
5580+
ASSERT_EQUALS(expected, tok(code));
5581+
}
55645582
}
55655583

55665584
void templateAlias1() {

test/testunusedvar.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5781,6 +5781,17 @@ class TestUnusedVar : public TestFixture {
57815781
" std::array<int, 1> a;\n"
57825782
"}\n");
57835783
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: a\n", errout.str());
5784+
5785+
functionVariableUsage("class A {};\n" // #9471
5786+
" namespace std {\n"
5787+
" template<>\n"
5788+
" struct hash<A> {};\n"
5789+
"}\n"
5790+
"char f() {\n"
5791+
" std::string hash = \"-\";\n"
5792+
" return hash[0];\n"
5793+
"}\n");
5794+
ASSERT_EQUALS("", errout.str());
57845795
}
57855796

57865797
void localvarFuncPtr() {

0 commit comments

Comments
 (0)