diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ea8faed1e..73a31bba6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5166,19 +5166,28 @@ void Tokenizer::simplifyVarDecl(bool only_k_r_fpar) tok2 = tok2->tokAt(2); if (tok2 && tok2->previous()->str() == "::") continue; - size_t indentlevel = 1; + unsigned int indentlevel = 0; + unsigned int parens = 0; for (Token *tok3 = tok2; tok3; tok3 = tok3->next()) { ++typelen; - if (tok3->str() == "<") { + if (tok3->str() == "<" && !parens) { ++indentlevel; - } else if (tok3->str() == ">") { - --indentlevel; - if (indentlevel == 0) { + } else if (tok3->str() == ">" && !parens) { + if (!indentlevel) { tok2 = tok3->next(); break; } + --indentlevel; + } else if (tok3->str() == "(") { + ++parens; + } else if (tok3->str() == ")") { + if (!parens) { + tok2 = NULL; + break; + } + --parens; } else if (tok3->str() == ";") { break; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6eb1371af..698e57857 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4426,6 +4426,8 @@ private: const char code1[] = "b<(1<<24),10,24> u, v;"; const char res1[] = "b < ( 1 << 24 ) , 10 , 24 > u ; b < ( 1 << 24 ) , 10 , 24 > v ;"; ASSERT_EQUALS(res1, tokenizeAndStringify(code1)); + // ticket #3571 (segmentation fault) + tokenizeAndStringify("template 4) > class X4 {};"); } void vardecl_union() {