From dab09aedeea4c9ced66f55af96ba0b3f6fb2317c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 17 Mar 2011 21:45:03 +0100 Subject: [PATCH] Fixed #2648 (Tokenizer::simplifyTemplates: Segmentation fault (gcc-testsuite)) --- lib/tokenize.cpp | 7 +++++++ test/testsimplifytokens.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b9bdfe11a..2c5f56d1e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index ecf51131c..20de55940 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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 struct B\n" + "{\n" + " int a[n];\n" + "};\n" + "\n" + "template 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..