Fixed #2648 (Tokenizer::simplifyTemplates: Segmentation fault (gcc-testsuite))

This commit is contained in:
Daniel Marjamäki 2011-03-17 21:45:03 +01:00
parent 7e04ea0859
commit dab09aedee
2 changed files with 32 additions and 0 deletions

View File

@ -2911,6 +2911,7 @@ void Tokenizer::simplifyTemplatesInstantiate(const Token *tok,
if (Token::Match(tok, "%var% ,|>"))
type.push_back(tok);
}
// bail out if the end of the file was reached
if (!tok)
return;
@ -3006,6 +3007,12 @@ void Tokenizer::simplifyTemplatesInstantiate(const Token *tok,
std::string s1(name + " < ");
for (const Token *tok3 = tok2->tokAt(2); tok3 && tok3->str() != ">"; tok3 = tok3->next())
{
// #2648 - unhandled paranthesis => bail out
if (tok3->str() == "(")
{
s.clear();
break;
}
if (!tok3->next())
{
s.clear();

View File

@ -115,6 +115,7 @@ private:
TEST_CASE(template22);
TEST_CASE(template23);
TEST_CASE(template24); // #2648 - using sizeof in template parameter
TEST_CASE(template25); // #2648 - another test for sizeof template parameter
TEST_CASE(template_unhandled);
TEST_CASE(template_default_parameter);
TEST_CASE(template_default_type);
@ -2060,6 +2061,30 @@ private:
ASSERT_EQUALS(expected, sizeof_(code));
}
void template25()
{
const char code[] = "template<int n> struct B\n"
"{\n"
" int a[n];\n"
"};\n"
"\n"
"template<int x> class bitset: B<((sizeof(int)) ? : 1)>\n"
"{};\n"
"\n"
"bitset<1> z;";
const char actual[] = "; bitset<1> z ; "
"class bitset<1> : B < ( ) > { }";
const char expected[] = "; "
"bitset<1> z ; "
"class bitset<1> : B<4> { } "
"struct B<4> { int a [ 4 ] ; }";
TODO_ASSERT_EQUALS(expected, actual, sizeof_(code));
}
void template_unhandled()
{
// An unhandled template usage should be simplified..