From 07e8325e50cc5a99651cc9baf56edfa38ed13765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 12 Feb 2011 16:51:59 +0100 Subject: [PATCH] Fixed #2549 (segmentation fault of cppcheck) --- lib/tokenize.cpp | 17 +++++++++++++++-- test/testtokenize.cpp | 13 +++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 352363f67..eb527394b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2780,8 +2780,21 @@ void Tokenizer::simplifyTemplates() { tok->insertToken(","); tok = tok->next(); - tok->insertToken((*it)->strAt(1)); - tok = tok->next(); + const Token *from = (*it)->next(); + std::stack links; + while (from && (!links.empty() || (from->str() != "," && from->str() != ">"))) + { + tok->insertToken(from->str()); + tok = tok->next(); + if (Token::Match(tok, "(|[")) + links.push(tok); + else if (!links.empty() && Token::Match(tok, ")|]")) + { + Token::createMutualLinks(links.top(), tok); + links.pop(); + } + from = from->next(); + } ++it; } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ae11bd0ed..e07d202b0 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -264,6 +264,7 @@ private: TEST_CASE(removeattribute); TEST_CASE(cpp0xtemplate1); TEST_CASE(cpp0xtemplate2); + TEST_CASE(cpp0xtemplate3); TEST_CASE(cpp0xdefault); TEST_CASE(arraySize); @@ -4634,6 +4635,18 @@ private: "list < list < int >> ints ;", tokenizeAndStringify(code)); } + void cpp0xtemplate3() + { + // #2549 + const char *code = "template\n" + "struct S\n" + "{};\n" + "S s;\n"; + TODO_ASSERT_EQUALS(";\n\n\nS < int , ( int ) 0 > s ;", // wanted result + ";\n\n\nS < int , ( T ) 0 > s ;", // current result + tokenizeAndStringify(code)); + } + void cpp0xdefault() { {