From e146591b5da40625cc30486861d8ee342072424c Mon Sep 17 00:00:00 2001 From: August Sodora Date: Sat, 2 Jun 2012 16:15:12 +0200 Subject: [PATCH] Fixed #3544 (segmentation fault of cppcheck) --- lib/tokenize.cpp | 3 +++ test/testtokenize.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a034928fb..040ebc7ff 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -116,6 +116,9 @@ Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last, if (tok2->str() == "(" || tok2->str() == "[" || tok2->str() == "{") links.push(tok2); else if (tok2->str() == ")" || tok2->str() == "]" || tok2->str() == "}") { + if (links.empty()) + return tok2; + Token * link = links.top(); tok2->link(link); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 5930eee3c..967aaa88a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -60,6 +60,7 @@ private: // don't freak out when the syntax is wrong TEST_CASE(wrong_syntax1); TEST_CASE(wrong_syntax2); + TEST_CASE(wrong_syntax3); // #3544 TEST_CASE(wrong_syntax_if_macro); // #2518 - if MACRO() TEST_CASE(minus); @@ -658,6 +659,18 @@ private: tokenizeAndStringify(code); } + void wrong_syntax3() { // #3544 + const char code[] = "X #define\n" + "{\n" + " (\n" + " for( #endif typedef typedef cb[N] )\n" + " ca[N]; = cb[i]\n" + " )\n" + "}"; + + tokenizeAndStringify(code); + } + void wrong_syntax_if_macro() { // #2518 const std::string code("void f() { if MACRO(); }");