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(); }");