From 8e5c63104c05f17943f6f86b296db236fd1132c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 7 May 2011 14:23:14 +0200 Subject: [PATCH] Fixed #2743 (segmentation fault of cppcheck 'std::vector v1 = a, v2 = b;') --- lib/tokenize.cpp | 14 +++++++++++++- test/testtokenize.cpp | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index cd421666a..7535c857b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -236,10 +236,22 @@ unsigned int Tokenizer::sizeOfType(const Token *type) const void Tokenizer::insertTokens(Token *dest, const Token *src, unsigned int n) { + std::stack link; + while (n > 0) { - dest->insertToken(src->str().c_str()); + dest->insertToken(src->str()); dest = dest->next(); + + // Set links + if (Token::Match(dest, "(|[|{")) + link.push(dest); + else if (!link.empty() && Token::Match(dest, ")|]|}")) + { + Token::createMutualLinks(dest, link.top()); + link.pop(); + } + dest->fileIndex(src->fileIndex()); dest->linenr(src->linenr()); dest->varId(src->varId()); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 97c628b94..6bc29510b 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -249,6 +249,7 @@ private: TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_template); TEST_CASE(vardecl_union); + TEST_CASE(vardecl_par); // #2743 - set links if variable type contains parentheses TEST_CASE(volatile_variables); TEST_CASE(syntax_error); TEST_CASE(syntax_error_templates_1); @@ -4096,6 +4097,18 @@ private: ASSERT_EQUALS("void f ( ) {\n\nint x ;\nlong & y = x ;\n\n}", tokenizeAndStringify(code2)); } + void vardecl_par() + { + // ticket #2743 - set links if variable type contains parentheses + const char code[] = "Fred fred1=a, fred2=b;"; + + Settings settings; + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp", "", false); + ASSERT_EQUALS(true, tokenizer.validate()); + } + void vardec_static() { {