From e07801a891e8e4be967cc45e14f7915b01d03864 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 4 Jan 2020 03:45:24 -0600 Subject: [PATCH] Fix issue 9563: new daca crash: findLambdaEndToken not finding end token (#2472) --- lib/symboldatabase.cpp | 5 ++++- lib/tokenlist.cpp | 6 ++++-- test/testtokenize.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index ed06da17c..df3aff0a5 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3431,7 +3431,10 @@ void Function::addArguments(const SymbolDatabase *symbolDatabase, const Scope *s if (tok->str() == "=") { initArgCount++; if (tok->strAt(1) == "[") { - tok = findLambdaEndToken(tok->next()); + const Token* lambdaStart = tok->next(); + tok = findLambdaEndToken(lambdaStart); + if (!tok) + throw InternalError(lambdaStart, "Analysis failed (lambda not recognized). If the code is valid then please report this failure.", InternalError::INTERNAL); } } } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 401b2c0ce..ad7b22bc3 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -765,6 +765,8 @@ static void compileTerm(Token *&tok, AST_state& state) while (Token::Match(tok, "%name%|%str%")) tok = tok->next(); } + if (Token::Match(tok, "%name% %assign%")) + tok = tok->next(); } } else if (tok->str() == "{") { const Token *prev = tok->previous(); @@ -1265,8 +1267,8 @@ static void createAstAtTokenInner(Token * const tok1, const Token *endToken, boo const Token * const endToken2 = tok->link(); for (; tok && tok != endToken && tok != endToken2; tok = tok ? tok->next() : nullptr) tok = createAstAtToken(tok, cpp); - } else if (Token::simpleMatch(tok->link(), "] (") && Token::Match(tok->link()->linkAt(1), ") .|{")) { - Token *bodyStart = tok->link()->linkAt(1)->next(); + } else if (const Token * lambdaEnd = findLambdaEndScope(tok)) { + Token *bodyStart = lambdaEnd->link(); if (Token::Match(bodyStart, ". %name%") && bodyStart->originalName() == "->") { bodyStart = bodyStart->next(); while (Token::Match(bodyStart, "%name%|::")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 689eb653d..a14468c82 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -8168,6 +8168,14 @@ private: " if (a::d) {}\n" " });\n" "}\n")) + + // #9563 + ASSERT_NO_THROW(tokenizeAndStringify("template struct a;\n" + "template struct a {\n" + " template a(d);\n" + "};\n" + "void e(\n" + " int, a f = [] {});\n")) } void checkIfCppCast() { ASSERT_NO_THROW(tokenizeAndStringify("struct a {\n"