From 5b687cb038e1fc20d961aa79b43cc3371ff44dcb Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 12 Dec 2022 22:58:48 +0100 Subject: [PATCH] Fix #11423 Crash in valueFlowForwardConst() (#4634) --- lib/tokenize.cpp | 10 +++++++++- test/testvarid.cpp | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2ab670f8e..60c0136bb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3917,7 +3917,15 @@ void Tokenizer::setVarIdPass1() if (!scopeStack.top().isExecutable) newFunctionDeclEnd = isFunctionHead(tok, "{:;"); else { - Token const * const tokenLinkNext = tok->link()->next(); + const Token* tokenLinkNext = tok->link()->next(); + if (Token::simpleMatch(tokenLinkNext, ".")) { // skip trailing return type + tokenLinkNext = tokenLinkNext->next(); + while (Token::Match(tokenLinkNext, "%name%|::")) { + tokenLinkNext = tokenLinkNext->next(); + if (Token::simpleMatch(tokenLinkNext, "<") && tokenLinkNext->link()) + tokenLinkNext = tokenLinkNext->link()->next(); + } + } if (tokenLinkNext && tokenLinkNext->str() == "{") // might be for- or while-loop or if-statement newFunctionDeclEnd = tokenLinkNext; } diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 5c9e1f881..c817864ac 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -176,6 +176,7 @@ private: TEST_CASE(varid_lambda_mutable); TEST_CASE(varid_trailing_return1); // #8889 TEST_CASE(varid_trailing_return2); // #9066 + TEST_CASE(varid_trailing_return3); // #11423 TEST_CASE(varid_parameter_pack); // #9383 TEST_CASE(varid_for_auto_cpp17); TEST_CASE(varid_not); // #9689 'not x' @@ -2822,6 +2823,18 @@ private: ASSERT_EQUALS(exp1, tokenize(code1)); } + void varid_trailing_return3() { // #11423 + const char code[] = "void f(int a, int b) {\n" + " auto g = [](int& a, const int b) -> void {};\n" + " auto h = [&a, &b]() { std::swap(a, b); };\n" + "}\n"; + const char exp[] = "1: void f ( int a@1 , int b@2 ) {\n" + "2: auto g@3 ; g@3 = [ ] ( int & a@4 , const int b@5 ) . void { } ;\n" + "3: auto h@6 ; h@6 = [ & a@1 , & b@2 ] ( ) { std :: swap ( a@1 , b@2 ) ; } ;\n" + "4: }\n"; + ASSERT_EQUALS(exp, tokenize(code)); + } + void varid_parameter_pack() { // #9383 const char code1[] = "template \n" "void func(Rest... parameters) {\n"