Skip to content

Commit 5b687cb

Browse files
Fix #11423 Crash in valueFlowForwardConst() (#4634)
1 parent 3f50540 commit 5b687cb

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3917,7 +3917,15 @@ void Tokenizer::setVarIdPass1()
39173917
if (!scopeStack.top().isExecutable)
39183918
newFunctionDeclEnd = isFunctionHead(tok, "{:;");
39193919
else {
3920-
Token const * const tokenLinkNext = tok->link()->next();
3920+
const Token* tokenLinkNext = tok->link()->next();
3921+
if (Token::simpleMatch(tokenLinkNext, ".")) { // skip trailing return type
3922+
tokenLinkNext = tokenLinkNext->next();
3923+
while (Token::Match(tokenLinkNext, "%name%|::")) {
3924+
tokenLinkNext = tokenLinkNext->next();
3925+
if (Token::simpleMatch(tokenLinkNext, "<") && tokenLinkNext->link())
3926+
tokenLinkNext = tokenLinkNext->link()->next();
3927+
}
3928+
}
39213929
if (tokenLinkNext && tokenLinkNext->str() == "{") // might be for- or while-loop or if-statement
39223930
newFunctionDeclEnd = tokenLinkNext;
39233931
}

test/testvarid.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class TestVarID : public TestFixture {
176176
TEST_CASE(varid_lambda_mutable);
177177
TEST_CASE(varid_trailing_return1); // #8889
178178
TEST_CASE(varid_trailing_return2); // #9066
179+
TEST_CASE(varid_trailing_return3); // #11423
179180
TEST_CASE(varid_parameter_pack); // #9383
180181
TEST_CASE(varid_for_auto_cpp17);
181182
TEST_CASE(varid_not); // #9689 'not x'
@@ -2822,6 +2823,18 @@ class TestVarID : public TestFixture {
28222823
ASSERT_EQUALS(exp1, tokenize(code1));
28232824
}
28242825

2826+
void varid_trailing_return3() { // #11423
2827+
const char code[] = "void f(int a, int b) {\n"
2828+
" auto g = [](int& a, const int b) -> void {};\n"
2829+
" auto h = [&a, &b]() { std::swap(a, b); };\n"
2830+
"}\n";
2831+
const char exp[] = "1: void f ( int a@1 , int b@2 ) {\n"
2832+
"2: auto g@3 ; g@3 = [ ] ( int & a@4 , const int b@5 ) . void { } ;\n"
2833+
"3: auto h@6 ; h@6 = [ & a@1 , & b@2 ] ( ) { std :: swap ( a@1 , b@2 ) ; } ;\n"
2834+
"4: }\n";
2835+
ASSERT_EQUALS(exp, tokenize(code));
2836+
}
2837+
28252838
void varid_parameter_pack() { // #9383
28262839
const char code1[] = "template <typename... Rest>\n"
28272840
"void func(Rest... parameters) {\n"

0 commit comments

Comments
 (0)